@@ -642,12 +642,14 @@ abstract class FlutterCommand extends Command<void> {
642642 }
643643
644644 void useDartDefineConfigJsonFileOption () {
645- argParser.addOption (
645+ argParser.addMultiOption (
646646 FlutterOptions .kDartDefineFromFileOption,
647647 help: 'The path of a json format file where flutter define a global constant pool. '
648648 'Json entry will be available as constants from the String.fromEnvironment, bool.fromEnvironment, '
649- 'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.' ,
650- valueHelp: 'use-define-config.json'
649+ 'int.fromEnvironment, and double.fromEnvironment constructors; the key and field are json values.\n '
650+ 'Multiple defines can be passed by repeating "--${FlutterOptions .kDartDefineFromFileOption }" multiple times.' ,
651+ valueHelp: 'use-define-config.json' ,
652+ splitCommas: false ,
651653 );
652654 }
653655
@@ -1185,9 +1187,8 @@ abstract class FlutterCommand extends Command<void> {
11851187 ? stringArgDeprecated (FlutterOptions .kPerformanceMeasurementFile)
11861188 : null ;
11871189
1188- List <String > dartDefines = argParser.options.containsKey (FlutterOptions .kDartDefinesOption)
1189- ? stringsArg (FlutterOptions .kDartDefinesOption)
1190- : < String > [];
1190+ final Map <String , Object >? defineConfigJsonMap = extractDartDefineConfigJsonMap ();
1191+ List <String > dartDefines = extractDartDefines (defineConfigJsonMap: defineConfigJsonMap);
11911192
11921193 WebRendererMode webRenderer = WebRendererMode .autoDetect;
11931194 if (argParser.options.containsKey (FlutterOptions .kWebRendererFlag)) {
@@ -1198,27 +1199,6 @@ abstract class FlutterCommand extends Command<void> {
11981199 dartDefines = updateDartDefines (dartDefines, webRenderer);
11991200 }
12001201
1201- Map <String , Object >? defineConfigJsonMap;
1202- if (argParser.options.containsKey (FlutterOptions .kDartDefineFromFileOption)) {
1203- final String ? configJsonPath = stringArg (FlutterOptions .kDartDefineFromFileOption);
1204- if (configJsonPath != null && globals.fs.isFileSync (configJsonPath)) {
1205- final String configJsonRaw = globals.fs.file (configJsonPath).readAsStringSync ();
1206- try {
1207- defineConfigJsonMap = < String , Object > {};
1208- // Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
1209- (json.decode (configJsonRaw) as Map <String , dynamic >).forEach ((String key, dynamic value) {
1210- defineConfigJsonMap? [key]= value as Object ;
1211- });
1212- defineConfigJsonMap.forEach ((String key, Object value) {
1213- dartDefines.add ('$key =$value ' );
1214- });
1215- } on FormatException catch (err) {
1216- throwToolExit ('Json config define file "--${FlutterOptions .kDartDefineFromFileOption }=$configJsonPath " format err, '
1217- 'please fix first! format err:\n $err ' );
1218- }
1219- }
1220- }
1221-
12221202 return BuildInfo (buildMode,
12231203 argParser.options.containsKey ('flavor' )
12241204 ? stringArgDeprecated ('flavor' )
@@ -1334,6 +1314,56 @@ abstract class FlutterCommand extends Command<void> {
13341314 }
13351315 }
13361316
1317+ List <String > extractDartDefines ({Map <String , Object >? defineConfigJsonMap}) {
1318+ final List <String > dartDefines = < String > [];
1319+
1320+ if (argParser.options.containsKey (FlutterOptions .kDartDefinesOption)) {
1321+ dartDefines.addAll (stringsArg (FlutterOptions .kDartDefinesOption));
1322+ }
1323+
1324+ if (defineConfigJsonMap == null ) {
1325+ return dartDefines;
1326+ }
1327+ defineConfigJsonMap.forEach ((String key, Object value) {
1328+ dartDefines.add ('$key =$value ' );
1329+ });
1330+
1331+ return dartDefines;
1332+ }
1333+
1334+ Map <String , Object >? extractDartDefineConfigJsonMap () {
1335+ final Map <String , Object > dartDefineConfigJsonMap = < String , Object > {};
1336+
1337+ if (argParser.options.containsKey (FlutterOptions .kDartDefineFromFileOption)) {
1338+ final List <String > configJsonPaths = stringsArg (
1339+ FlutterOptions .kDartDefineFromFileOption,
1340+ );
1341+
1342+ for (final String path in configJsonPaths) {
1343+ if (! globals.fs.isFileSync (path)) {
1344+ throwToolExit ('Json config define file "--${FlutterOptions
1345+ .kDartDefineFromFileOption }=$path " is not a file, '
1346+ 'please fix first!' );
1347+ }
1348+
1349+ final String configJsonRaw = globals.fs.file (path).readAsStringSync ();
1350+ try {
1351+ // Fix json convert Object value :type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Object>' in type cast
1352+ (json.decode (configJsonRaw) as Map <String , dynamic >)
1353+ .forEach ((String key, dynamic value) {
1354+ dartDefineConfigJsonMap[key] = value as Object ;
1355+ });
1356+ } on FormatException catch (err) {
1357+ throwToolExit ('Json config define file "--${FlutterOptions
1358+ .kDartDefineFromFileOption }=$path " format err, '
1359+ 'please fix first! format err:\n $err ' );
1360+ }
1361+ }
1362+ }
1363+
1364+ return dartDefineConfigJsonMap;
1365+ }
1366+
13371367 /// Updates dart-defines based on [webRenderer] .
13381368 @visibleForTesting
13391369 static List <String > updateDartDefines (List <String > dartDefines, WebRendererMode webRenderer) {
0 commit comments