Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

--dart-define-from-file values are empty or the command fails

ErrorDid not find the file passed to "--dart-define-from-file". Path: config.json

Short 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.

  • Applies to Flutter 3.3x, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'));
  2. 2Match what you see, then jump to its fix

    1. 1No error, but String.fromEnvironment returns the default value or an empty stringThe value is read without constFix
    2. 2The value is the default only for some keysThe key in the file does not match the key in DartFix
    3. 3Did 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. 4is used by the framework and cannot be set using --dart-define or --dart-define-from-fileA key is reserved by the Flutter frameworkFix

What does the error look like?

  • Did not find the file passed to "--dart-define-from-file". Path: config.json
  • Unable 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 const

2Fix

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.json

Confirmed by dart.dev

Did this fix it?

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" lib

2Fix

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.json

Confirmed by dart.dev

Did this fix it?

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.json

2Fix

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=true

3Verify

The command starts building instead of printing a parse error.

flutter run --dart-define-from-file=config.json

Confirmed by raw.githubusercontent.com

Did this fix it?

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.json

2Fix

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.json

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.