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

"Environment variable not found for field" if build_runner hasn't been run without envied #59

Closed
jamesncl opened this issue Sep 19, 2023 · 9 comments · Fixed by #79
Closed
Labels
duplicate This issue or pull request already exists

Comments

@jamesncl
Copy link

Whenever I create a fresh clone of my project, which uses envied, when I run flutter pub run build_runner build --delete-conflicting-outputs I always get:

Environment variable not found for field accessToken.
package:my_project/env/env.dart:8:23

8 │ static final String accessToken = _Env.accessToken;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^

The only way I can resolve the problem is to comment out my env.dart file, run build_runner (which succeeds), then restore my env.dart and run build_runner again. Then the problem seems to fix itself. So basically, envied doesn't seem to work if you haven't run build_runner at least once without any envied code.

I'm using Flutter 3.13.1. My variables are stored in a file called .env at the root of my project.

Here's my env.dart:

import 'package:envied/envied.dart';

part 'env.g.dart';

@Envied(path: '.env')
abstract class Env {
  @EnviedField(varName: 'ACCESS_TOKEN', obfuscate: true)
  static final String accessToken = _Env.accessToken;
}

And my .env:

ACCESS_TOKEN=my-token
@DarkHeros09
Copy link

DarkHeros09 commented Sep 20, 2023

yep, i got the same problem.

@DarkHeros09
Copy link

Any update on this issue? the package is not working right now.

[✓] Flutter (Channel stable, 3.13.4, on Microsoft Windows [Version 10.0.22621.2283], locale en-US)
    • Flutter version 3.13.4 on channel stable at C:\Users\m_ben\.puro\envs\stable\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 367f9ea16b (11 days ago), 2023-09-12 23:27:53 -0500
    • Engine revision 9064459a8b
    • Dart version 3.1.2
    • DevTools version 2.25.0

@atanasyordanov21
Copy link

I have the same problem when adding new variables in my .env file. It always says that the new variable was not found. But as @jamesncl proposed, if I comment out the entire env.dart file (as if it doesn't exist at all), then run build_runner, then bring back env.dart (remove comments), then again build_runner - everything comes to its place. Anyway thanks for the great plugin!

@Saeeed-B
Copy link

@atanasyordanov21 not this is not solution , and not workig.

@Saeeed-B
Copy link

@petercinibulk please see this issue

@sonjz
Copy link

sonjz commented Oct 25, 2023

The issue is a known build_runner cache related issue, there are 2 fixes:

this is a hammer:

more delicate:

@techouse
Copy link
Collaborator

Duplicate of #6

@techouse techouse marked this as a duplicate of #6 Oct 26, 2023
@techouse techouse added the duplicate This issue or pull request already exists label Nov 6, 2023
@jamesncl
Copy link
Author

build_runner clean does appear to be a workaround, thanks. Should this be added to the envied README? Because as far as I can tell this package is essentially unusable without this crucial piece of knowledge. Alternatively, this issue and #6 should be left open until the root cause is fixed, so others can understand and find the workaround.

@techouse techouse linked a pull request Nov 14, 2023 that will close this issue
@techouse
Copy link
Collaborator

Should this be added to the envied README?

Good idea! Added in #77

@techouse techouse linked a pull request Nov 19, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
6 participants