Home > front end >  How to put API_KEY inside the ".env" file in a Flutter app?
How to put API_KEY inside the ".env" file in a Flutter app?

Time:05-20

I want to put my API key inside the .env file within my Flutter application and I did it like following:

API_KEY=ewwfwfwe
API_KEY = ewwfwfwe
API_KEY=`ewwfwfwe`
API_KEY = `ewwfwfwe`

But none of them works! I don't know what is the problem and which way is the right way doing that?

CodePudding user response:

The very first one is correct, just make sure you're that in your pubspec.yaml under assets you added .env then in your main function you have something that looks like this.

void main() async {
  await dotenv.load(fileName: ".env");
  runApp(MyApp());
}

Then in the places you want to use the .env variables, you call them like this

dotenv.env["API_KEY"]!
  • Related