How to securely store API keys in Flutter?

3,823

You can do this by using the flutter_dotenv package.
Create a .env file in the root of your project:

GOOGLE_API = API_KEY

Add the .env file to your assets bundle in pubspec.yaml

assets:
  - .env

Add the .env file as an entry in your .gitignore if it isn't already

.env*

Access variables from .env throughout the application

DotEnv().env['GOOGLE_API'];

Read more on how to install and use the package here: flutter_dotenv

Share:
3,823
Benjamin
Author by

Benjamin

Hi! I'm a sophomore in High School and I love computers & programming. I'm familiar with Flutter & Dart. I also do web development and am pretty good at HTML, CSS, and JS. I typically use the Angular framework with TypeScript. TL;DR Languages I know: HTML, CSS, JS Dart Lua TypeScript (if you count that as a language) Frameworks I know: Angular Flutter Svelte

Updated on December 14, 2022

Comments

  • Benjamin
    Benjamin over 1 year

    I'm using the googleapi's Flutter package and to use the Google Drive API, I need to put in credentials. My question is, how can I securely store them in my app so when I publish my app, they can't be taken. I found a cool package, flutter_secure_storage but to use it, I need to put all of my values into the secure storage. So how can I do that? I was thinking of using something like this, but I'm not sure. It would be great if someone could put me in the right direction as to how to do this by the book so to speak.

    To further explain, I don't want to have my sensitive information in a file such as main.dart as a variable to put into storage (if it isn't there already).