Flutter ENVied package build runner error
Asked Answered
C

3

7

The problem I am facing is with ENVied package for flutter. I am trying to save my api keys in environment variables.

I followed these steps.

  1. Installed these three packages
$ flutter pub add envied
$ flutter pub add --dev envied_generator
$ flutter pub add --dev build_runner
  1. I created .env file and saved my api key in it. File Structure

  2. After this, I created the env.dart file with the following code:

import 'package:envied/envied.dart';

part 'env.g.dart';

@Envied(path: '.env')
abstract class Env {
  @EnviedField(varName: 'TEST_KEY')
  static final String apiKey = _Env.apiKey;
}

  1. Then I ran the build runner command. dart run build_runner build

After this command I faced the error saying Envied requires types to be explicitly declared. 'apiKey' does not declare a type.

  1. So I made changes in my code and declared a type of String and ran the command again.

Now I faced another build runner error stating Environment variable not found for field 'apiKey'.

And a env.g.dart file is not generated.

I have tried a few times and from what I can tell I am following the set up on pub.dev exactly, so not sure how to fix it. I checked out the possible issue here but still none of those responses helped me with this issue.

Carbonate answered 24/11, 2023 at 0:34 Comment(0)
F
4
import 'package:envied/envied.dart';

part 'env.g.dart';

@Envied(path: '.env') //give full path here with name like secret.env instead of only extension
abstract class Env {
@EnviedField(varName: 'varNameHere')
static String varNameHere = _Env.varNameHere;
}

the variables name should be the same at all three places. Here in your case your varName is 'TEST_KEY', and variable storing name is apikey. so replace all three places with same name as in example above and run command:

dart run build_runner build --delete-conflicting-outputs

this will generate all files necessary & follow the pub.dev envied instructions. This article may be helpful securing API keys in flutter

Fate answered 29/12, 2023 at 6:30 Comment(1)
I also faced the same error. I had to move the .env file to the root of the application before it worked. To those who don't know what the root folder is, just locate the pubspec.yaml file and move the .env file into the same folder that contains the pubspec.yaml file. But my question is this; if specifying the full path here like this @Envied(path: 'lib/util/.env') will not work until you moved it into the root folder and still specify the path like this @Envied(path: '.env'); then what is the essence of specifying the path?Matchwood
M
2

I had the same problem, I solved it by moving my .env file at the root of my project. It seems to help ENVied to detect the file.

Microcline answered 10/6 at 6:1 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked.Integration
Thank you, this is the only thing that helped me. You just need to move .env file from lib/env/ to the same directory as lib folder.Finzer
M
0

Just to add more to what has already been shared, you can further tighten the security of your API keys by obfuscating it. This is what I mean

import 'package:envied/envied.dart';

part 'env.g.dart';

@Envied(path: '.env.dev')
abstract class Env {
  @EnviedField(varName: 'GOOGLE_MAP_API_KEY', obfuscate: true)
  static final String googleMapApiKey = _Env.googleMapApiKey;
  @EnviedField(varName: 'FACEBOOK_API_KEY', obfuscate: true)
  static final String facebookApiKey = _Env.facebookApiKey;
  @EnviedField(varName: 'AWS_API_KEY', obfuscate: true)
  static final String awsApiKey = _Env.awsApiKey;
}

Then you run the command

dart run build_runner build --delete-conflicting-outputs

The key here is the "obfuscate: true". This way it hashes you API keys and cannot be easily extracted.

Matchwood answered 21/8 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.