4

I want to use an API key in my flutter app, and have read that the envied package is a good tool to keep the API key hidden.

The issue is I can't get this working. This is what I've done:

  1. Installed the 3 packages:
$ flutter pub add envied
$ flutter pub add --dev envied_generator
$ flutter pub add --dev build_runner
  1. Created a file in the root folder named .env to store the key
API_KEY=1234567890
  1. Created a class in lib/env/env.dart
import 'package:envied/envied.dart';

part 'env.g.dart';

@Envied(path: '.env')
abstract class Env {
  @EnviedField(varName: 'API_KEY')
  static final apiKey = _Env.apiKey;
}
  1. Then run:
flutter pub run build_runner build

And then I get this error:

[SEVERE] envied_generator:envied on lib/env/env.dart:

Envied can only handle types such as `int`, `double`, `num`, `bool` and `String`. Type `InvalidType` is not one of them.
  ╷
9 │   static final apiKey = _Env.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.

5 Answers 5

13

For the obfuscator to work, you need to specify a variable type. Example:

static final String apiKey = _Env.apiKey;
Sign up to request clarification or add additional context in comments.

1 Comment

This does remove the error indeed, but the env.g.dart file is not generated
2

I had the same problem, i.e. I used API_KEY=....

Changing to APIKEY (without underscore) solved my problem.

Comments

2

build_runner probably cached the previous result that failed. You should clean up build_runner cache and try to re-generate the env.g.dart file:

flutter pub run build_runner clean
flutter pub run build_runner build

Comments

1

For me I got this error when I was trying to nest my files into a lower folder

lib/util/env/env.dart

This didn't work but the generator ran when I changed my file location to the following:

lib/env/env.dart

Comments

0

Envied can only handle types such as int, double, num, bool and String. Type InvalidType is not one of them.

If you got this kind of error: It simply saying you need to declare the type:

static final String key = _Env.key;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.