Skip to main content
Cinemapedia reads sensitive configuration values — such as the TheMovieDB API key — from a .env file at runtime. The file is loaded before the app starts using the flutter_dotenv package.

Prerequisites

Make sure flutter_dotenv is listed in your pubspec.yaml dependencies and that the .env file is declared as a Flutter asset:
pubspec.yaml

Creating the .env file

Create a .env file in the root of the project (next to pubspec.yaml) with the following content:
.env
Replace your_api_key_here with a valid API key from TheMovieDB.
Never commit the .env file to version control. It contains secret API keys that should not be exposed in a public repository. Add .env to your .gitignore file immediately.
.gitignore

How the .env file is loaded

main() is declared async and calls dotenv.load() before runApp, guaranteeing that environment variables are available as soon as any widget or service requests them.
lib/main.dart

The Environment class

All environment variable access is centralised in a single class so the rest of the app never imports flutter_dotenv directly.
lib/config/constants/environment.dart
If THE_MOVIEDB_KEY is missing from the .env file, the field falls back to the string 'No hay api key'. API calls will fail in this state, so always verify the key is present before running the app.

Environment variables reference

string
required
API key for TheMovieDB. Used to authenticate every HTTP request made to the TMDB REST API. Obtain a free key by creating an account and requesting API access in your account settings.

Using the value in your code

Import the Environment class wherever you need the key:
Because theMovieDbKey is a static field it is read once when the class is first loaded. dotenv.load() must complete before any code accesses Environment.theMovieDbKey, which is why it is awaited in main().