--dart-define-from-file values are empty or the command fails
Did not find the file passed to "--dart-define-from-file". Path: config.jsonShort answer
Values from --dart-define-from-file are compile-time declarations. They only reach Dart through const String.fromEnvironment with the exact same key. Empty values usually mean a non-const lookup, a key mismatch, or the flag missing from one run, build or IDE launch configuration. The tool also rejects bad files with its own errors.
Fix it fast
1Find out which cause you have
Run the app from a terminal with the flag spelled out and print the value once. That separates a bad file or flag from a Dart-side lookup problem.
flutter run --dart-define-from-file=config.json # in Dart: print(const String.fromEnvironment('API_URL', defaultValue: 'NOT SET'));2Match what you see, then jump to its fix
- 1
No error, but String.fromEnvironment returns the default value or an empty stringThe value is read without constFix - 2
The value is the default only for some keysThe key in the file does not match the key in DartFix - 3
Did not find the file passed to "--dart-define-from-file" or Unable to parse the file ... Ensure that the file contains valid JSONThe file path is wrong or the file is not validFix - 4
is used by the framework and cannot be set using --dart-define or --dart-define-from-fileA key is reserved by the Flutter frameworkFix
- 1
What does the error look like?
Did not find the file passed to "--dart-define-from-file". Path: config.jsonUnable to parse the file at path "config.json" due to a formatting error. Ensure that the file contains valid JSON.Unable to parse file provided for --dart-define-from-file.is used by the framework and cannot be set using --dart-define or --dart-define-from-file
Cause 1 of 4
The value is read without const
1Check
Environment declaration constructors are only guaranteed to work when invoked as const, because compilers evaluate them at compile time.
grep -rn "fromEnvironment" lib | grep -v const2Fix
Call the constructor in a const context and use the same key as in the file.
const apiUrl = String.fromEnvironment('API_URL');
const debugMode = bool.fromEnvironment('DEBUG');3Verify
Run with the flag again and print the constants.
flutter run --dart-define-from-file=config.jsonConfirmed by dart.dev
Cause 2 of 4
The key in the file does not match the key in Dart
1Check
Keys are matched by exact name, including case. Compare the file with every fromEnvironment call.
cat config.json
grep -rn "fromEnvironment" lib2Fix
Rename one side so both are identical. Declarations are passed as NAME=VALUE pairs, so each key in the JSON file becomes one name.
{
"API_URL": "https://api.example.com",
"DEBUG": true
}3Verify
Restart the app fully; a hot reload does not re-read compile-time values.
flutter run --dart-define-from-file=config.jsonConfirmed by dart.dev
Cause 3 of 4
The file path is wrong or the file is not valid
1Check
The path is read relative to where you run the command. The tool decides the format from the content: text starting with { is parsed as JSON, anything else is read as a .env-style file.
ls -l config.json
python3 -m json.tool config.json2Fix
Fix the path, then fix the syntax the message names: valid JSON for a JSON file; for a .env file, one KEY=value per line, # comments allowed, and no multi-line values. The file must be UTF-8 or UTF-16.
API_URL=https://api.example.com
DEBUG=true3Verify
The command starts building instead of printing a parse error.
flutter run --dart-define-from-file=config.jsonConfirmed by raw.githubusercontent.com
Cause 4 of 4
A key is reserved by the Flutter framework
1Check
The message starts with the key name. Framework keys such as FLUTTER_VERSION cannot be set from a file.
grep -n "FLUTTER_" config.json2Fix
Rename your own key, for example APP_FLUTTER_VERSION, and read the new name in Dart.
{
"APP_ENV": "staging"
}3Verify
Run the command again.
flutter run --dart-define-from-file=config.jsonConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Pass the same flag to flutter run, flutter build and the IDE run configuration, or the values differ between them.
- Read every define once in a single config file as const values and import that.
- Keep secrets out of the repository; the values are compiled into the app and are not protected.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix