Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with Multiple Generations for the Same Class When Using strict-casts #133

Open
fernandoptr opened this issue Jan 22, 2025 · 0 comments

Comments

@fernandoptr
Copy link

Description

The new feature of Multiple Generations for the Same Class #124 introduces an issue when the strict-casts language rule is enabled in the analysis_options.yaml. This causes a type error when generating code using build_runner.

Steps to Reproduce

Create app_env_fields.dart:

abstract interface class AppEnvFields {
  abstract final String apiUrl;
}

Create envs.env.dart:

import 'package:envied/envied.dart';
import 'app_env_fields.dart';

part `'envs.env.g.dart'`;

@Envied(path: '.env.production', name: 'Prod')
@Envied(path: '.env.staging', name: 'Staging')
final class Envs implements AppEnvFields {
  factory Envs() => _instance;

  static const bool kDebugMode = true;

  static final dynamic _instance = kDebugMode ? _Staging() : _Prod();

  @override
  @EnviedField(varName: 'REST_API_URL')
  final String apiUrl = _instance.apiUrl;
}

Add strict-casts to analysis_options.yaml:

analyzer:
  language:
    strict-casts: true

Run build_runner:

flutter pub run build_runner build

Expected Behavior

The code should generate successfully, and the Envs class should work without any linting or type errors.

Actual Behavior

The following error occurs:

A value of type 'dynamic' can't be returned from the constructor 'Envs' because it has a return type of 'Envs'.dart(return_of_invalid_type)

The issue is caused by the line:

factory Envs() => _instance;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant