diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index 5260a3ddf6..053197c8f1 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -38,7 +38,7 @@ class BuildInfo { this.webRenderer = WebRendererMode.auto, required this.treeShakeIcons, this.performanceMeasurementFile, - this.dartDefineConfigJsonMap, + this.dartDefineConfigJsonMap = const {}, this.packagesPath = '.dart_tool/package_config.json', // TODO(zanderso): make this required and remove the default. this.nullSafetyMode = NullSafetyMode.sound, this.codeSizeDirectory, @@ -144,7 +144,7 @@ class BuildInfo { /// /// An additional field `dartDefineConfigJsonMap` is provided to represent the native JSON value of the configuration file /// - final Map? dartDefineConfigJsonMap; + final Map dartDefineConfigJsonMap; /// If provided, an output directory where one or more v8-style heap snapshots /// will be written for code size profiling. @@ -267,7 +267,7 @@ class BuildInfo { /// Fields that are `null` are excluded from this configuration. Map toEnvironmentConfig() { final Map map = {}; - dartDefineConfigJsonMap?.forEach((String key, Object value) { + dartDefineConfigJsonMap.forEach((String key, Object? value) { map[key] = '$value'; }); final Map environmentMap = { @@ -327,17 +327,15 @@ class BuildInfo { for (final String projectArg in androidProjectArgs) '-P$projectArg', ]; - if (dartDefineConfigJsonMap != null) { - final Iterable gradleConfKeys = result.map((final String gradleConf) => gradleConf.split('=')[0].substring(2)); - dartDefineConfigJsonMap!.forEach((String key, Object value) { - if (gradleConfKeys.contains(key)) { - globals.printWarning( - 'The key: [$key] already exists, you cannot use gradle variables that have been used by the system!'); - } else { - result.add('-P$key=$value'); - } - }); - } + final Iterable gradleConfKeys = result.map((final String gradleConf) => gradleConf.split('=')[0].substring(2)); + dartDefineConfigJsonMap.forEach((String key, Object? value) { + if (gradleConfKeys.contains(key)) { + globals.printWarning( + 'The key: [$key] already exists, you cannot use gradle variables that have been used by the system!'); + } else { + result.add('-P$key=$value'); + } + }); return result; } } diff --git a/packages/flutter_tools/lib/src/bundle_builder.dart b/packages/flutter_tools/lib/src/bundle_builder.dart index afa1530fc0..680eac7c67 100644 --- a/packages/flutter_tools/lib/src/bundle_builder.dart +++ b/packages/flutter_tools/lib/src/bundle_builder.dart @@ -21,7 +21,6 @@ import 'devfs.dart'; import 'globals.dart' as globals; import 'project.dart'; - /// Provides a `build` method that builds the bundle. class BundleBuilder { /// Builds the bundle for the given target platform. diff --git a/packages/flutter_tools/lib/src/commands/assemble.dart b/packages/flutter_tools/lib/src/commands/assemble.dart index ba2b528dd7..1f9a212c07 100644 --- a/packages/flutter_tools/lib/src/commands/assemble.dart +++ b/packages/flutter_tools/lib/src/commands/assemble.dart @@ -258,7 +258,7 @@ class AssembleCommand extends FlutterCommand { results[kExtraGenSnapshotOptions] = (argumentResults[FlutterOptions.kExtraGenSnapshotOptions] as List).join(','); } - final Map? defineConfigJsonMap = extractDartDefineConfigJsonMap(); + final Map defineConfigJsonMap = extractDartDefineConfigJsonMap(); final List dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap); if (dartDefines.isNotEmpty){ results[kDartDefines] = dartDefines.join(','); diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index daf1c467fa..0e1c2af642 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. - - import 'dart:async'; import 'package:meta/meta.dart'; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index a099d81379..333d2c68b5 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -1190,7 +1190,7 @@ abstract class FlutterCommand extends Command { ? stringArg(FlutterOptions.kPerformanceMeasurementFile) : null; - final Map? defineConfigJsonMap = extractDartDefineConfigJsonMap(); + final Map defineConfigJsonMap = extractDartDefineConfigJsonMap(); List dartDefines = extractDartDefines(defineConfigJsonMap: defineConfigJsonMap); WebRendererMode webRenderer = WebRendererMode.auto; @@ -1323,29 +1323,26 @@ abstract class FlutterCommand extends Command { } } - List extractDartDefines({Map? defineConfigJsonMap}) { + List extractDartDefines({required Map defineConfigJsonMap}) { final List dartDefines = []; if (argParser.options.containsKey(FlutterOptions.kDartDefinesOption)) { dartDefines.addAll(stringsArg(FlutterOptions.kDartDefinesOption)); } - if (defineConfigJsonMap == null) { - return dartDefines; - } - defineConfigJsonMap.forEach((String key, Object value) { + defineConfigJsonMap.forEach((String key, Object? value) { dartDefines.add('$key=$value'); }); return dartDefines; } - Map? extractDartDefineConfigJsonMap() { - final Map dartDefineConfigJsonMap = {}; + Map extractDartDefineConfigJsonMap() { + final Map dartDefineConfigJsonMap = {}; if (argParser.options.containsKey(FlutterOptions.kDartDefineFromFileOption)) { final List configJsonPaths = stringsArg( - FlutterOptions.kDartDefineFromFileOption, + FlutterOptions.kDartDefineFromFileOption, ); for (final String path in configJsonPaths) { @@ -1359,8 +1356,8 @@ abstract class FlutterCommand extends Command { try { // Fix json convert Object value :type '_InternalLinkedHashMap' is not a subtype of type 'Map' in type cast (json.decode(configJsonRaw) as Map) - .forEach((String key, dynamic value) { - dartDefineConfigJsonMap[key] = value as Object; + .forEach((String key, Object? value) { + dartDefineConfigJsonMap[key] = value; }); } on FormatException catch (err) { throwToolExit('Json config define file "--${FlutterOptions diff --git a/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart b/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart index db99b2b75e..f0d9714beb 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:typed_data'; + import 'package:args/command_runner.dart'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/file_system.dart'; @@ -12,6 +14,7 @@ import 'package:flutter_tools/src/bundle.dart'; import 'package:flutter_tools/src/bundle_builder.dart'; import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/commands/build_bundle.dart'; +import 'package:flutter_tools/src/convert.dart'; import 'package:flutter_tools/src/features.dart'; import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/project.dart'; @@ -504,7 +507,7 @@ void main() { ProcessManager: () => FakeProcessManager.any(), }); - testUsingContext('test --dart-define-from-file option', () async { + testUsingContext('--dart-define-from-file successfully forwards values to build env', () async { globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true); globals.fs.file('pubspec.yaml').createSync(); globals.fs.file('.packages').createSync(); @@ -514,7 +517,8 @@ void main() { "kInt": 1, "kDouble": 1.1, "name": "denghaizhu", - "title": "this is title from config json file" + "title": "this is title from config json file", + "nullValue": null } ''' ); @@ -537,7 +541,17 @@ void main() { ]); }, overrides: { BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) { - expect(environment.defines[kDartDefines], 'a0ludD0x,a0RvdWJsZT0xLjE=,bmFtZT1kZW5naGFpemh1,dGl0bGU9dGhpcyBpcyB0aXRsZSBmcm9tIGNvbmZpZyBqc29uIGZpbGU=,Ym9keT10aGlzIGlzIGJvZHkgZnJvbSBjb25maWcganNvbiBmaWxl'); + expect( + _decodeDartDefines(environment), + containsAllInOrder(const [ + 'kInt=1', + 'kDouble=1.1', + 'name=denghaizhu', + 'title=this is title from config json file', + 'nullValue=null', + 'body=this is body from config json file', + ]), + ); }), FileSystem: fsFactory, ProcessManager: () => FakeProcessManager.any(), @@ -576,7 +590,10 @@ void main() { ]); }, overrides: { BuildSystem: () => TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) { - expect(environment.defines[kDartDefines], 'a0ludD0y,a0RvdWJsZT0xLjE=,bmFtZT1kZW5naGFpemh1,dGl0bGU9dGhpcyBpcyB0aXRsZSBmcm9tIGNvbmZpZyBqc29uIGZpbGU='); + expect( + _decodeDartDefines(environment), + containsAllInOrder(['kInt=2', 'kDouble=1.1', 'name=denghaizhu', 'title=this is title from config json file']), + ); }), FileSystem: fsFactory, ProcessManager: () => FakeProcessManager.any(), @@ -632,6 +649,15 @@ void main() { }); } +Iterable _decodeDartDefines(Environment environment) { + final String encodedDefines = environment.defines[kDartDefines]!; + const Utf8Decoder byteDecoder = Utf8Decoder(); + return encodedDefines + .split(',') + .map(base64.decode) + .map(byteDecoder.convert); +} + class FakeBundleBuilder extends Fake implements BundleBuilder { @override Future build({