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:
$ flutter pub add envied
$ flutter pub add --dev envied_generator
$ flutter pub add --dev build_runner
API_KEY=1234567890
import 'package:envied/envied.dart';
part 'env.g.dart';
@Envied(path: '.env')
abstract class Env {
@EnviedField(varName: 'API_KEY')
static final apiKey = _Env.apiKey;
}
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.
For the obfuscator to work, you need to specify a variable type. Example:
static final String apiKey = _Env.apiKey;