Problem setting up ENVIED package in Flutter to hide API key
Asked Answered
E

5

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.

Expert answered 8/7, 2023 at 4:44 Comment(0)
D
13

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

static final String apiKey = _Env.apiKey;
Digged answered 11/7, 2023 at 8:42 Comment(1)
This does remove the error indeed, but the env.g.dart file is not generatedJohnnyjumpup
T
2

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

Changing to APIKEY (without underscore) solved my problem.

Twi answered 23/10, 2023 at 14:24 Comment(0)
T
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
Terrorist answered 3/2 at 14:57 Comment(0)
M
1

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
Matthewmatthews answered 22/7 at 12:3 Comment(0)
M
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;
Mint answered 11/9, 2023 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.