diff --git a/packages/flutter_tools/lib/src/asset.dart b/packages/flutter_tools/lib/src/asset.dart index c5fa45ab289..ed42baea293 100644 --- a/packages/flutter_tools/lib/src/asset.dart +++ b/packages/flutter_tools/lib/src/asset.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. -// @dart = 2.8 - import 'package:meta/meta.dart'; import 'package:package_config/package_config.dart'; @@ -48,12 +46,12 @@ const List> kMaterialFonts = >[ /// Injected factory class for spawning [AssetBundle] instances. abstract class AssetBundleFactory { /// The singleton instance, pulled from the [AppContext]. - static AssetBundleFactory get instance => context.get(); + static AssetBundleFactory get instance => context.get()!; static AssetBundleFactory defaultInstance({ - @required Logger logger, - @required FileSystem fileSystem, - @required Platform platform, + required Logger logger, + required FileSystem fileSystem, + required Platform platform, bool splitDeferredAssets = false, }) => _ManifestAssetBundleFactory(logger: logger, fileSystem: fileSystem, platform: platform, splitDeferredAssets: splitDeferredAssets); @@ -82,18 +80,18 @@ abstract class AssetBundle { /// Returns 0 for success; non-zero for failure. Future build({ String manifestPath = defaultManifestPath, - String assetDirPath, - @required String packagesPath, + String? assetDirPath, + required String packagesPath, bool deferredComponentsEnabled = false, - TargetPlatform targetPlatform, + TargetPlatform? targetPlatform, }); } class _ManifestAssetBundleFactory implements AssetBundleFactory { _ManifestAssetBundleFactory({ - @required Logger logger, - @required FileSystem fileSystem, - @required Platform platform, + required Logger logger, + required FileSystem fileSystem, + required Platform platform, bool splitDeferredAssets = false, }) : _logger = logger, _fileSystem = fileSystem, @@ -114,9 +112,9 @@ class ManifestAssetBundle implements AssetBundle { /// Constructs an [ManifestAssetBundle] that gathers the set of assets from the /// pubspec.yaml manifest. ManifestAssetBundle({ - @required Logger logger, - @required FileSystem fileSystem, - @required Platform platform, + required Logger logger, + required FileSystem fileSystem, + required Platform platform, bool splitDeferredAssets = false, }) : _logger = logger, _fileSystem = fileSystem, @@ -144,7 +142,7 @@ class ManifestAssetBundle implements AssetBundle { // the current project. final Map _wildcardDirectories = {}; - DateTime _lastBuildTimestamp; + DateTime? _lastBuildTimestamp; static const String _kAssetManifestJson = 'AssetManifest.json'; static const String _kNoticeFile = 'NOTICES'; @@ -161,7 +159,7 @@ class ManifestAssetBundle implements AssetBundle { @override bool needsBuild({ String manifestPath = defaultManifestPath }) { - final DateTime lastBuildTimestamp = _lastBuildTimestamp; + final DateTime? lastBuildTimestamp = _lastBuildTimestamp; if (lastBuildTimestamp == null) { return true; } @@ -192,10 +190,10 @@ class ManifestAssetBundle implements AssetBundle { @override Future build({ String manifestPath = defaultManifestPath, - String assetDirPath, - @required String packagesPath, + String? assetDirPath, + required String packagesPath, bool deferredComponentsEnabled = false, - TargetPlatform targetPlatform, + TargetPlatform? targetPlatform, }) async { assetDirPath ??= getAssetBuildDirectory(); FlutterProject flutterProject; @@ -244,7 +242,7 @@ class ManifestAssetBundle implements AssetBundle { if (flutterProject.linux.existsSync()) flutterProject.linux.managedDirectory.path, ]; - final Map<_Asset, List<_Asset>> assetVariants = _parseAssets( + final Map<_Asset, List<_Asset>>? assetVariants = _parseAssets( packageConfig, flutterManifest, wildcardDirectories, @@ -274,7 +272,7 @@ class ManifestAssetBundle implements AssetBundle { } final bool includesMaterialFonts = flutterManifest.usesMaterialDesign; - final List> fonts = _parseFonts( + final List> fonts = _parseFonts( flutterManifest, packageConfig, primary: true, @@ -287,7 +285,7 @@ class ManifestAssetBundle implements AssetBundle { if (packageUri != null && packageUri.scheme == 'file') { final String packageManifestPath = _fileSystem.path.fromUri(packageUri.resolve('../pubspec.yaml')); inputFiles.add(_fileSystem.file(packageManifestPath)); - final FlutterManifest packageFlutterManifest = FlutterManifest.createFromPath( + final FlutterManifest? packageFlutterManifest = FlutterManifest.createFromPath( packageManifestPath, logger: _logger, fileSystem: _fileSystem, @@ -309,7 +307,7 @@ class ManifestAssetBundle implements AssetBundle { } final String packageBasePath = _fileSystem.path.dirname(packageManifestPath); - final Map<_Asset, List<_Asset>> packageAssets = _parseAssets( + final Map<_Asset, List<_Asset>>? packageAssets = _parseAssets( packageConfig, packageFlutterManifest, // Do not track wildcard directories for dependencies. @@ -344,11 +342,12 @@ class ManifestAssetBundle implements AssetBundle { // asset in entries. for (final _Asset asset in assetVariants.keys) { final File assetFile = asset.lookupAssetFile(_fileSystem); - if (!assetFile.existsSync() && assetVariants[asset].isEmpty) { + final List<_Asset> variants = assetVariants[asset]!; + if (!assetFile.existsSync() && variants.isEmpty) { _logger.printStatus('Error detected in pubspec.yaml:', emphasis: true); _logger.printError('No file or variants found for $asset.\n'); if (asset.package != null) { - _logger.printError('This asset was included from package ${asset.package.name}.'); + _logger.printError('This asset was included from package ${asset.package?.name}.'); } return 1; } @@ -359,10 +358,10 @@ class ManifestAssetBundle implements AssetBundle { // "1x" resolution variant and if both exist then the explicit 1x // variant is preferred. if (assetFile.existsSync()) { - assert(!assetVariants[asset].contains(asset)); - assetVariants[asset].insert(0, asset); + assert(!variants.contains(asset)); + variants.insert(0, asset); } - for (final _Asset variant in assetVariants[asset]) { + for (final _Asset variant in variants) { final File variantFile = variant.lookupAssetFile(_fileSystem); inputFiles.add(variantFile); assert(variantFile.existsSync()); @@ -374,13 +373,14 @@ class ManifestAssetBundle implements AssetBundle { if (deferredComponentsAssetVariants != null) { for (final String componentName in deferredComponentsAssetVariants.keys) { deferredComponentsEntries[componentName] = {}; - for (final _Asset asset in deferredComponentsAssetVariants[componentName].keys) { + final Map<_Asset, List<_Asset>> assetsMap = deferredComponentsAssetVariants[componentName]!; + for (final _Asset asset in assetsMap.keys) { final File assetFile = asset.lookupAssetFile(_fileSystem); - if (!assetFile.existsSync() && deferredComponentsAssetVariants[componentName][asset].isEmpty) { + if (!assetFile.existsSync() && assetsMap[asset]!.isEmpty) { _logger.printStatus('Error detected in pubspec.yaml:', emphasis: true); _logger.printError('No file or variants found for $asset.\n'); if (asset.package != null) { - _logger.printError('This asset was included from package ${asset.package.name}.'); + _logger.printError('This asset was included from package ${asset.package?.name}.'); } return 1; } @@ -391,13 +391,13 @@ class ManifestAssetBundle implements AssetBundle { // "1x" resolution variant and if both exist then the explicit 1x // variant is preferred. if (assetFile.existsSync()) { - assert(!deferredComponentsAssetVariants[componentName][asset].contains(asset)); - deferredComponentsAssetVariants[componentName][asset].insert(0, asset); + assert(!assetsMap[asset]!.contains(asset)); + assetsMap[asset]!.insert(0, asset); } - for (final _Asset variant in deferredComponentsAssetVariants[componentName][asset]) { + for (final _Asset variant in assetsMap[asset]!) { final File variantFile = variant.lookupAssetFile(_fileSystem); assert(variantFile.existsSync()); - deferredComponentsEntries[componentName][variant.entryUri.path] ??= DevFSFileContent(variantFile); + deferredComponentsEntries[componentName]![variant.entryUri.path] ??= DevFSFileContent(variantFile); } } } @@ -455,7 +455,7 @@ class ManifestAssetBundle implements AssetBundle { entries[key] = content; return; } - final DevFSStringContent oldContent = entries[key] as DevFSStringContent; + final DevFSStringContent? oldContent = entries[key] as DevFSStringContent?; if (oldContent?.string != content.string) { entries[key] = content; } @@ -463,7 +463,7 @@ class ManifestAssetBundle implements AssetBundle { void _setLicenseIfChanged( String combinedLicenses, - TargetPlatform targetPlatform, + TargetPlatform? targetPlatform, ) { // On the web, don't compress the NOTICES file since the client doesn't have // dart:io to decompress it. So use the standard _setIfChanged to check if @@ -478,7 +478,7 @@ class ManifestAssetBundle implements AssetBundle { // the uncompressed strings to not incur decompression/decoding while making // the comparison. if (!entries.containsKey(_kNoticeZippedFile) || - (entries[_kNoticeZippedFile] as DevFSStringCompressingBytesContent) + (entries[_kNoticeZippedFile] as DevFSStringCompressingBytesContent?) ?.equals(combinedLicenses) != true) { entries[_kNoticeZippedFile] = DevFSStringCompressingBytesContent( combinedLicenses, @@ -493,18 +493,18 @@ class ManifestAssetBundle implements AssetBundle { List<_Asset> _getMaterialAssets() { final List<_Asset> result = <_Asset>[]; for (final Map family in kMaterialFonts) { - final Object fonts = family['fonts']; + final Object? fonts = family['fonts']; if (fonts == null) { continue; } for (final Map font in fonts as List>) { - final String asset = font['asset'] as String; + final String? asset = font['asset'] as String?; if (asset == null) { continue; } final Uri entryUri = _fileSystem.path.toUri(asset); result.add(_Asset( - baseDir: _fileSystem.path.join(Cache.flutterRoot, 'bin', 'cache', 'artifacts', 'material_fonts'), + baseDir: _fileSystem.path.join(Cache.flutterRoot!, 'bin', 'cache', 'artifacts', 'material_fonts'), relativeUri: Uri(path: entryUri.pathSegments.last), entryUri: entryUri, package: null, @@ -515,13 +515,13 @@ class ManifestAssetBundle implements AssetBundle { return result; } - List> _parseFonts( + List> _parseFonts( FlutterManifest manifest, PackageConfig packageConfig, { - String packageName, - @required bool primary, + String? packageName, + required bool primary, }) { - return >[ + return >[ if (primary && manifest.usesMaterialDesign) ...kMaterialFonts, if (packageName == null) @@ -543,7 +543,7 @@ class ManifestAssetBundle implements AssetBundle { Directory projectDirectory, { List excludeDirs = const [], }) { - final List components = flutterManifest.deferredComponents; + final List? components = flutterManifest.deferredComponents; final Map>> deferredComponentsAssetVariants = >>{}; if (components == null) { return deferredComponentsAssetVariants; @@ -559,7 +559,7 @@ class ManifestAssetBundle implements AssetBundle { flutterManifest, assetBasePath, cache, - deferredComponentsAssetVariants[component.name], + deferredComponentsAssetVariants[component.name]!, assetUri, excludeDirs: excludeDirs, ); @@ -569,7 +569,7 @@ class ManifestAssetBundle implements AssetBundle { flutterManifest, assetBasePath, cache, - deferredComponentsAssetVariants[component.name], + deferredComponentsAssetVariants[component.name]!, assetUri, excludeDirs: excludeDirs, ); @@ -604,7 +604,7 @@ class ManifestAssetBundle implements AssetBundle { final List<_Asset> sortedKeys = jsonEntries.keys.toList() ..sort((_Asset left, _Asset right) => left.entryUri.path.compareTo(right.entryUri.path)); for (final _Asset main in sortedKeys) { - jsonObject[main.entryUri.path] = jsonEntries[main]; + jsonObject[main.entryUri.path] = jsonEntries[main]!; } return DevFSStringContent(json.encode(jsonObject)); } @@ -623,7 +623,7 @@ class ManifestAssetBundle implements AssetBundle { final Uri assetUri = fontAsset.assetUri; if (assetUri.pathSegments.first == 'packages' && !_fileSystem.isFileSync(_fileSystem.path.fromUri( - packageConfig[packageName]?.packageUriRoot?.resolve('../${assetUri.path}')))) { + packageConfig[packageName]?.packageUriRoot.resolve('../${assetUri.path}')))) { packageFontAssets.add(FontAsset( fontAsset.assetUri, weight: fontAsset.weight, @@ -667,14 +667,14 @@ class ManifestAssetBundle implements AssetBundle { /// ], /// } /// ``` - Map<_Asset, List<_Asset>> _parseAssets( + Map<_Asset, List<_Asset>>? _parseAssets( PackageConfig packageConfig, FlutterManifest flutterManifest, List wildcardDirectories, String assetBase, { List excludeDirs = const [], - String packageName, - Package attributedPackage, + String? packageName, + Package? attributedPackage, }) { final Map<_Asset, List<_Asset>> result = <_Asset, List<_Asset>>{}; @@ -737,8 +737,8 @@ class ManifestAssetBundle implements AssetBundle { Map<_Asset, List<_Asset>> result, Uri assetUri, { List excludeDirs = const [], - String packageName, - Package attributedPackage, + String? packageName, + Package? attributedPackage, }) { final String directoryPath = _fileSystem.path.join( assetBase, assetUri.toFilePath(windows: _platform.isWindows)); @@ -777,8 +777,8 @@ class ManifestAssetBundle implements AssetBundle { Map<_Asset, List<_Asset>> result, Uri assetUri, { List excludeDirs = const [], - String packageName, - Package attributedPackage, + String? packageName, + Package? attributedPackage, }) { final _Asset asset = _resolveAsset( packageConfig, @@ -792,7 +792,7 @@ class ManifestAssetBundle implements AssetBundle { for (final String path in cache.variantsFor(assetFile.path)) { final String relativePath = _fileSystem.path.relative(path, from: asset.baseDir); final Uri relativeUri = _fileSystem.path.toUri(relativePath); - final Uri entryUri = asset.symbolicPrefixUri == null + final Uri? entryUri = asset.symbolicPrefixUri == null ? relativeUri : asset.symbolicPrefixUri?.resolveUri(relativeUri); if (entryUri != null) { @@ -814,15 +814,15 @@ class ManifestAssetBundle implements AssetBundle { PackageConfig packageConfig, String assetsBaseDir, Uri assetUri, - String packageName, - Package attributedPackage, + String? packageName, + Package? attributedPackage, ) { final String assetPath = _fileSystem.path.fromUri(assetUri); if (assetUri.pathSegments.first == 'packages' && !_fileSystem.isFileSync(_fileSystem.path.join(assetsBaseDir, assetPath))) { // The asset is referenced in the pubspec.yaml as // 'packages/PACKAGE_NAME/PATH/TO/ASSET . - final _Asset packageAsset = _resolvePackageAsset( + final _Asset? packageAsset = _resolvePackageAsset( assetUri, packageConfig, attributedPackage, @@ -842,12 +842,12 @@ class ManifestAssetBundle implements AssetBundle { ); } - _Asset _resolvePackageAsset(Uri assetUri, PackageConfig packageConfig, Package attributedPackage) { + _Asset? _resolvePackageAsset(Uri assetUri, PackageConfig packageConfig, Package? attributedPackage) { assert(assetUri.pathSegments.first == 'packages'); if (assetUri.pathSegments.length > 1) { final String packageName = assetUri.pathSegments[1]; - final Package package = packageConfig[packageName]; - final Uri packageUri = package?.packageUriRoot; + final Package? package = packageConfig[packageName]; + final Uri? packageUri = package?.packageUriRoot; if (packageUri != null && packageUri.scheme == 'file') { return _Asset( baseDir: _fileSystem.path.fromUri(packageUri), @@ -869,15 +869,15 @@ class ManifestAssetBundle implements AssetBundle { @immutable class _Asset { const _Asset({ - @required this.baseDir, - @required this.relativeUri, - @required this.entryUri, - @required this.package, + required this.baseDir, + required this.relativeUri, + required this.entryUri, + required this.package, }); final String baseDir; - final Package package; + final Package? package; /// A platform-independent URL where this asset can be found on disk on the /// host system relative to [baseDir]. @@ -892,7 +892,7 @@ class _Asset { /// The delta between what the entryUri is and the relativeUri (e.g., /// packages/flutter_gallery). - Uri get symbolicPrefixUri { + Uri? get symbolicPrefixUri { if (entryUri == relativeUri) { return null; } @@ -966,11 +966,11 @@ class _AssetDirectoryCache { continue; } variants[variantName] ??= []; - variants[variantName].add(path); + variants[variantName]!.add(path); } _cache[directory] = variants; } - return _cache[directory][assetName] ?? const []; + return _cache[directory]![assetName] ?? const []; } } diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart index 97176dfaa20..73da9c2d345 100644 --- a/packages/flutter_tools/lib/src/compile.dart +++ b/packages/flutter_tools/lib/src/compile.dart @@ -471,8 +471,8 @@ abstract class ResidentCompiler { List? invalidatedFiles, { required String outputPath, required PackageConfig packageConfig, - required String projectRootPath, required FileSystem fs, + String? projectRootPath, bool suppressErrors = false, bool checkDartPluginRegistry = false, }); diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index c254f5c3508..4aea017f700 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -2,11 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; -import 'package:meta/meta.dart'; import 'package:package_config/package_config.dart'; import 'package:vm_service/vm_service.dart' as vm_service; @@ -29,7 +26,7 @@ class DevFSConfig { bool noDirectorySymlinks = false; } -DevFSConfig get devFSConfig => context.get(); +DevFSConfig? get devFSConfig => context.get(); /// Common superclass for content copied to the device. abstract class DevFSContent { @@ -60,11 +57,11 @@ class DevFSFileContent extends DevFSContent { DevFSFileContent(this.file); final FileSystemEntity file; - File _linkTarget; - FileStat _fileStat; + File? _linkTarget; + FileStat? _fileStat; File _getFile() { - final File linkTarget = _linkTarget; + final File? linkTarget = _linkTarget; if (linkTarget != null) { return linkTarget; } @@ -76,7 +73,7 @@ class DevFSFileContent extends DevFSContent { } void _stat() { - final File linkTarget = _linkTarget; + final File? linkTarget = _linkTarget; if (linkTarget != null) { // Stat the cached symlink target. final FileStat fileStat = linkTarget.statSync(); @@ -106,9 +103,9 @@ class DevFSFileContent extends DevFSContent { @override bool get isModified { - final FileStat oldFileStat = _fileStat; + final FileStat? oldFileStat = _fileStat; _stat(); - final FileStat newFileStat = _fileStat; + final FileStat? newFileStat = _fileStat; if (oldFileStat == null && newFileStat == null) { return false; } @@ -117,9 +114,9 @@ class DevFSFileContent extends DevFSContent { @override bool isModifiedAfter(DateTime time) { - final FileStat oldFileStat = _fileStat; + final FileStat? oldFileStat = _fileStat; _stat(); - final FileStat newFileStat = _fileStat; + final FileStat? newFileStat = _fileStat; if (oldFileStat == null && newFileStat == null) { return false; } @@ -218,7 +215,7 @@ class DevFSStringContent extends DevFSByteContent { /// The `hintString` parameter is a zlib dictionary hinting mechanism to suggest /// the most common string occurrences to potentially assist with compression. class DevFSStringCompressingBytesContent extends DevFSContent { - DevFSStringCompressingBytesContent(this._string, { String hintString }) + DevFSStringCompressingBytesContent(this._string, { String? hintString }) : _compressor = ZLibEncoder( dictionary: hintString == null ? null @@ -231,10 +228,9 @@ class DevFSStringCompressingBytesContent extends DevFSContent { final ZLibEncoder _compressor; final DateTime _modificationTime = DateTime.now(); - List _bytes; bool _isModified = true; - List get bytes => _bytes ??= _compressor.convert(utf8.encode(_string)); + late final List bytes = _compressor.convert(utf8.encode(_string)); /// Return true only once so that the content is written to the device only once. @override @@ -266,7 +262,7 @@ class DevFSException implements Exception { DevFSException(this.message, [this.error, this.stackTrace]); final String message; final dynamic error; - final StackTrace stackTrace; + final StackTrace? stackTrace; @override String toString() => 'DevFSException($message, $error, $stackTrace)'; @@ -286,10 +282,10 @@ class _DevFSHttpWriter implements DevFSWriter { _DevFSHttpWriter( this.fsName, FlutterVmService serviceProtocol, { - @required OperatingSystemUtils osUtils, - @required HttpClient httpClient, - @required Logger logger, - Duration uploadRetryThrottle, + required OperatingSystemUtils osUtils, + required HttpClient httpClient, + required Logger logger, + Duration? uploadRetryThrottle, }) : httpAddress = serviceProtocol.httpAddress, _client = httpClient, @@ -300,10 +296,10 @@ class _DevFSHttpWriter implements DevFSWriter { final HttpClient _client; final OperatingSystemUtils _osUtils; final Logger _logger; - final Duration _uploadRetryThrottle; + final Duration? _uploadRetryThrottle; final String fsName; - final Uri httpAddress; + final Uri? httpAddress; // 3 was chosen to try to limit the variance in the time it takes to execute // `await request.close()` since there is a known bug in Dart where it doesn't @@ -312,11 +308,11 @@ class _DevFSHttpWriter implements DevFSWriter { static const int kMaxInFlight = 3; int _inFlight = 0; - Map _outstanding = {}; - Completer _completer = Completer(); + late Map _outstanding; + late Completer _completer; @override - Future write(Map entries, Uri devFSBase, [DevFSWriter parent]) async { + Future write(Map entries, Uri devFSBase, [DevFSWriter? parent]) async { try { _client.maxConnectionsPerHost = kMaxInFlight; _completer = Completer(); @@ -335,7 +331,7 @@ class _DevFSHttpWriter implements DevFSWriter { void _scheduleWrites() { while ((_inFlight < kMaxInFlight) && (!_completer.isCompleted) && _outstanding.isNotEmpty) { final Uri deviceUri = _outstanding.keys.first; - final DevFSContent content = _outstanding.remove(deviceUri); + final DevFSContent content = _outstanding.remove(deviceUri)!; _startWrite(deviceUri, content, retry: 10); _inFlight += 1; } @@ -351,7 +347,7 @@ class _DevFSHttpWriter implements DevFSWriter { }) async { while(true) { try { - final HttpClientRequest request = await _client.putUrl(httpAddress); + final HttpClientRequest request = await _client.putUrl(httpAddress!); request.headers.removeAll(HttpHeaders.acceptEncodingHeader); request.headers.add('dev_fs_name', fsName); request.headers.add('dev_fs_uri_b64', base64.encode(utf8.encode('$deviceUri'))); @@ -424,7 +420,7 @@ class UpdateFSReport { Duration get findInvalidatedDuration => _findInvalidatedDuration; bool _success; - String fastReassembleClassName; + String? fastReassembleClassName; int _invalidatedSourcesCount; int _syncedBytes; int _scannedSourcesCount; @@ -454,11 +450,11 @@ class DevFS { FlutterVmService serviceProtocol, this.fsName, this.rootDirectory, { - @required OperatingSystemUtils osUtils, - @required Logger logger, - @required FileSystem fileSystem, - HttpClient httpClient, - Duration uploadRetryThrottle, + required OperatingSystemUtils osUtils, + required Logger logger, + required FileSystem fileSystem, + HttpClient? httpClient, + Duration? uploadRetryThrottle, StopwatchFactory stopwatchFactory = const StopwatchFactory(), }) : _vmService = serviceProtocol, _logger = logger, @@ -471,7 +467,7 @@ class DevFS { uploadRetryThrottle: uploadRetryThrottle, httpClient: httpClient ?? ((context.get() == null) ? HttpClient() - : context.get()())), + : context.get()!())), _stopwatchFactory = stopwatchFactory; final FlutterVmService _vmService; @@ -488,13 +484,13 @@ class DevFS { bool hasSetAssetDirectory = false; List sources = []; - DateTime lastCompiled; - DateTime _previousCompiled; - PackageConfig lastPackageConfig; - File _widgetCacheOutputFile; + DateTime? lastCompiled; + DateTime? _previousCompiled; + PackageConfig? lastPackageConfig; + File? _widgetCacheOutputFile; - Uri _baseUri; - Uri get baseUri => _baseUri; + Uri? _baseUri; + Uri? get baseUri => _baseUri; Uri deviceUriToHostUri(Uri deviceUri) { final String deviceUriString = deviceUri.toString(); @@ -510,7 +506,7 @@ class DevFS { _logger.printTrace('DevFS: Creating new filesystem on the device ($_baseUri)'); try { final vm_service.Response response = await _vmService.createDevFS(fsName); - _baseUri = Uri.parse(response.json['uri'] as String); + _baseUri = Uri.parse(response.json!['uri'] as String); } on vm_service.RPCError catch (rpcException) { if (rpcException.code == RPCErrorCodes.kServiceDisappeared) { // This can happen if the device has been disconnected, so translate to @@ -526,10 +522,10 @@ class DevFS { _logger.printTrace('DevFS: Creating failed. Destroying and trying again'); await destroy(); final vm_service.Response response = await _vmService.createDevFS(fsName); - _baseUri = Uri.parse(response.json['uri'] as String); + _baseUri = Uri.parse(response.json!['uri'] as String); } _logger.printTrace('DevFS: Created new filesystem on the device ($_baseUri)'); - return _baseUri; + return _baseUri!; } Future destroy() async { @@ -556,8 +552,8 @@ class DevFS { /// /// If any other changes were made, or there is an error scanning the file, /// return `null`. - String _checkIfSingleWidgetReloadApplied() { - final File widgetCacheOutputFile = _widgetCacheOutputFile; + String? _checkIfSingleWidgetReloadApplied() { + final File? widgetCacheOutputFile = _widgetCacheOutputFile; if (widgetCacheOutputFile != null && widgetCacheOutputFile.existsSync()) { final String widget = widgetCacheOutputFile.readAsStringSync().trim(); if (widget.isNotEmpty) { @@ -571,20 +567,20 @@ class DevFS { /// /// Returns the number of bytes synced. Future update({ - @required Uri mainUri, - @required ResidentCompiler generator, - @required bool trackWidgetCreation, - @required String pathToReload, - @required List invalidatedFiles, - @required PackageConfig packageConfig, - @required String dillOutputPath, - DevFSWriter devFSWriter, - String target, - AssetBundle bundle, - DateTime firstBuildTime, + required Uri mainUri, + required ResidentCompiler generator, + required bool trackWidgetCreation, + required String pathToReload, + required List invalidatedFiles, + required PackageConfig packageConfig, + required String dillOutputPath, + DevFSWriter? devFSWriter, + String? target, + AssetBundle? bundle, + DateTime? firstBuildTime, bool bundleFirstUpload = false, bool fullRestart = false, - String projectRootPath, + String? projectRootPath, }) async { assert(trackWidgetCreation != null); assert(generator != null); @@ -606,7 +602,7 @@ class DevFS { // Await the compiler response after checking if the bundle is updated. This allows the file // stating to be done while waiting for the frontend_server response. final Stopwatch compileTimer = _stopwatchFactory.createStopwatch('compile')..start(); - final Future pendingCompilerOutput = generator.recompile( + final Future pendingCompilerOutput = generator.recompile( mainUri, invalidatedFiles, outputPath: dillOutputPath, @@ -614,7 +610,7 @@ class DevFS { projectRootPath: projectRootPath, packageConfig: packageConfig, checkDartPluginRegistry: true, // The entry point is assumed not to have changed. - ).then((CompilerOutput result) { + ).then((CompilerOutput? result) { compileTimer.stop(); return result; }); @@ -641,7 +637,7 @@ class DevFS { } }); } - final CompilerOutput compilerOutput = await pendingCompilerOutput; + final CompilerOutput? compilerOutput = await pendingCompilerOutput; if (compilerOutput == null || compilerOutput.errorCount > 0) { return UpdateFSReport(success: false); } @@ -654,8 +650,8 @@ class DevFS { // Don't send full kernel file that would overwrite what VM already // started loading from. if (!bundleFirstUpload) { - final String compiledBinary = compilerOutput?.outputFilename; - if (compiledBinary != null && compiledBinary.isNotEmpty) { + final String compiledBinary = compilerOutput.outputFilename; + if (compiledBinary.isNotEmpty) { final Uri entryUri = _fileSystem.path.toUri(pathToReload); final DevFSFileContent content = DevFSFileContent(_fileSystem.file(compiledBinary)); syncedBytes += content.size; @@ -665,7 +661,7 @@ class DevFS { _logger.printTrace('Updating files.'); final Stopwatch transferTimer = _stopwatchFactory.createStopwatch('transfer')..start(); if (dirtyEntries.isNotEmpty) { - await (devFSWriter ?? _httpWriter).write(dirtyEntries, _baseUri, _httpWriter); + await (devFSWriter ?? _httpWriter).write(dirtyEntries, _baseUri!, _httpWriter); } transferTimer.stop(); _logger.printTrace('DevFS: Sync finished'); @@ -693,16 +689,17 @@ class DevFS { /// Requires that the file system is the same for both the tool and application. class LocalDevFSWriter implements DevFSWriter { LocalDevFSWriter({ - @required FileSystem fileSystem, + required FileSystem fileSystem, }) : _fileSystem = fileSystem; final FileSystem _fileSystem; @override - Future write(Map entries, Uri baseUri, [DevFSWriter parent]) async { + Future write(Map entries, Uri baseUri, [DevFSWriter? parent]) async { try { - for (final Uri uri in entries.keys) { - final DevFSContent devFSContent = entries[uri]; + for (final MapEntry entry in entries.entries) { + final Uri uri = entry.key; + final DevFSContent devFSContent = entry.value; final File destination = _fileSystem.file(baseUri.resolveUri(uri)); if (!destination.parent.existsSync()) { destination.parent.createSync(recursive: true); diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 0e9d63e63a0..ad8eb6b9744 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.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. -// @dart = 2.8 - import 'dart:async'; import 'dart:math' as math; @@ -25,7 +23,7 @@ import 'device_port_forwarder.dart'; import 'project.dart'; import 'vmservice.dart'; -DeviceManager get deviceManager => context.get(); +DeviceManager? get deviceManager => context.get(); /// A description of the kind of workflow the device supports. class Category { @@ -63,9 +61,9 @@ class PlatformType { /// A discovery mechanism for flutter-supported development devices. abstract class DeviceManager { DeviceManager({ - @required Logger logger, - @required Terminal terminal, - @required UserMessages userMessages, + required Logger logger, + required Terminal terminal, + required UserMessages userMessages, }) : _logger = logger, _terminal = terminal, _userMessages = userMessages; @@ -78,17 +76,17 @@ abstract class DeviceManager { /// of their methods are called. List get deviceDiscoverers; - String _specifiedDeviceId; + String? _specifiedDeviceId; /// A user-specified device ID. - String get specifiedDeviceId { + String? get specifiedDeviceId { if (_specifiedDeviceId == null || _specifiedDeviceId == 'all') { return null; } return _specifiedDeviceId; } - set specifiedDeviceId(String id) { + set specifiedDeviceId(String? id) { _specifiedDeviceId = id; } @@ -120,7 +118,7 @@ abstract class DeviceManager { // found quickly, we don't wait for all the discoverers to complete. final List prefixMatches = []; final Completer exactMatchCompleter = Completer(); - final List>> futureDevices = >>[ + final List?>> futureDevices = ?>>[ for (final DeviceDiscovery discoverer in _platformDiscoverers) if (!hasWellKnownId || discoverer.wellKnownIds.contains(specifiedDeviceId)) discoverer @@ -143,9 +141,9 @@ abstract class DeviceManager { ]; // Wait for an exact match, or for all discoverers to return results. - await Future.any(>[ + await Future.any(>[ exactMatchCompleter.future, - Future.wait>(futureDevices), + Future.wait?>(futureDevices), ]); if (exactMatchCompleter.isCompleted) { @@ -156,9 +154,11 @@ abstract class DeviceManager { /// Returns the list of connected devices, filtered by any user-specified device id. Future> getDevices() { - return hasSpecifiedDeviceId - ? getDevicesById(specifiedDeviceId) - : getAllConnectedDevices(); + final String? id = specifiedDeviceId; + if (id == null) { + return getAllConnectedDevices(); + } + return getDevicesById(id); } Iterable get _platformDiscoverers { @@ -176,7 +176,7 @@ abstract class DeviceManager { } /// Returns the list of all connected devices. Discards existing cache of devices. - Future> refreshAllConnectedDevices({ Duration timeout }) async { + Future> refreshAllConnectedDevices({ Duration? timeout }) async { final List> devices = await Future.wait>(>>[ for (final DeviceDiscovery discoverer in _platformDiscoverers) discoverer.discoverDevices(timeout: timeout), @@ -214,7 +214,7 @@ abstract class DeviceManager { /// /// * If [flutterProject] is null, then assume the project supports all /// device types. - Future> findTargetDevices(FlutterProject flutterProject, { Duration timeout }) async { + Future> findTargetDevices(FlutterProject flutterProject, { Duration? timeout }) async { if (timeout != null) { // Reset the cache with the specified timeout. await refreshAllConnectedDevices(timeout: timeout); @@ -338,7 +338,7 @@ abstract class DeviceDiscovery { Future> get devices; /// Return all connected devices. Discards existing cache of devices. - Future> discoverDevices({ Duration timeout }); + Future> discoverDevices({ Duration? timeout }); /// Gets a list of diagnostic messages pertaining to issues with any connected /// devices (will be an empty list if there are no issues). @@ -366,11 +366,11 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { @protected @visibleForTesting - ItemListNotifier deviceNotifier; + ItemListNotifier? deviceNotifier; - Timer _timer; + Timer? _timer; - Future> pollingGetDevices({ Duration timeout }); + Future> pollingGetDevices({ Duration? timeout }); void startPolling() { if (_timer == null) { @@ -380,11 +380,11 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { } } - Timer _initTimer(Duration pollingTimeout) { + Timer _initTimer(Duration? pollingTimeout) { return Timer(_pollingInterval, () async { try { final List devices = await pollingGetDevices(timeout: pollingTimeout); - deviceNotifier.updateWithNewList(devices); + deviceNotifier!.updateWithNewList(devices); } on TimeoutException { // Do nothing on a timeout. } @@ -404,24 +404,24 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { } @override - Future> discoverDevices({ Duration timeout }) { + Future> discoverDevices({ Duration? timeout }) { deviceNotifier = null; return _populateDevices(timeout: timeout); } - Future> _populateDevices({ Duration timeout }) async { + Future> _populateDevices({ Duration? timeout }) async { deviceNotifier ??= ItemListNotifier.from(await pollingGetDevices(timeout: timeout)); - return deviceNotifier.items; + return deviceNotifier!.items; } Stream get onAdded { deviceNotifier ??= ItemListNotifier(); - return deviceNotifier.onAdded; + return deviceNotifier!.onAdded; } Stream get onRemoved { deviceNotifier ??= ItemListNotifier(); - return deviceNotifier.onRemoved; + return deviceNotifier!.onRemoved; } void dispose() => stopPolling(); @@ -436,9 +436,9 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { /// the host operating system in the case of Flutter Desktop. abstract class Device { Device(this.id, { - @required this.category, - @required this.platformType, - @required this.ephemeral, + required this.category, + required this.platformType, + required this.ephemeral, }); final String id; @@ -534,7 +534,7 @@ abstract class Device { /// /// For example, the desktop device classes can use a writer which /// copies the files across the local file system. - DevFSWriter createDevFSWriter( + DevFSWriter? createDevFSWriter( covariant ApplicationPackage app, String userIdentifier, ) { @@ -564,7 +564,7 @@ abstract class Device { void clearLogs(); /// Optional device-specific artifact overrides. - OverrideArtifacts get artifactOverrides => null; + OverrideArtifacts? get artifactOverrides => null; /// Start an app package on the current device. /// @@ -575,7 +575,7 @@ abstract class Device { String mainPath, String route, DebuggingOptions debuggingOptions, - Map platformArgs, + Map platformArgs, bool prebuiltApplication = false, bool ipv6 = false, String userIdentifier, @@ -817,8 +817,8 @@ class DebuggingOptions { final bool enableSoftwareRendering; final bool skiaDeterministicRendering; final bool traceSkia; - final String traceAllowlist; - final String traceSkiaAllowlist; + final String? traceAllowlist; + final String? traceSkiaAllowlist; final bool traceSystrace; final bool endlessTraceBuffer; final bool dumpSkpOnShaderCompilation; @@ -826,14 +826,14 @@ class DebuggingOptions { final bool purgePersistentCache; final bool useTestFonts; final bool verboseSystemLogs; - final int hostVmServicePort; - final int deviceVmServicePort; + final int? hostVmServicePort; + final int? deviceVmServicePort; final bool disablePortPublication; - final int ddsPort; - final Uri devToolsServerAddress; - final String port; - final String hostname; - final bool webEnableExposeUrl; + final int? ddsPort; + final Uri? devToolsServerAddress; + final String? port; + final String? hostname; + final bool? webEnableExposeUrl; final bool webUseSseForDebugProxy; final bool webUseSseForDebugBackend; final bool webUseSseForInjectedClient; @@ -846,13 +846,13 @@ class DebuggingOptions { final bool webRunHeadless; /// The port the browser should use for its debugging protocol. - final int webBrowserDebugPort; + final int? webBrowserDebugPort; /// Enable expression evaluation for web target. final bool webEnableExpressionEvaluation; /// A file where the VM Service URL should be written after the application is started. - final String vmserviceOutFile; + final String? vmserviceOutFile; final bool fastStart; final bool nullAssertions; @@ -875,7 +875,7 @@ class LaunchResult { bool get hasObservatory => observatoryUri != null; final bool started; - final Uri observatoryUri; + final Uri? observatoryUri; @override String toString() { @@ -896,13 +896,13 @@ abstract class DeviceLogReader { /// Some logs can be obtained from a VM service stream. /// Set this after the VM services are connected. - FlutterVmService connectedVMService; + FlutterVmService? connectedVMService; @override String toString() => name; /// Process ID of the app on the device. - int appPid; + int? appPid; // Clean up resources allocated by log reader e.g. subprocesses void dispose(); @@ -923,10 +923,10 @@ class NoOpDeviceLogReader implements DeviceLogReader { final String name; @override - int appPid; + int? appPid; @override - FlutterVmService connectedVMService; + FlutterVmService? connectedVMService; @override Stream get logLines => const Stream.empty(); @@ -939,7 +939,7 @@ class NoOpDeviceLogReader implements DeviceLogReader { /// [debuggingOptions.nullAssertions] is true. String computeDartVmFlags(DebuggingOptions debuggingOptions) { return [ - if (debuggingOptions.dartFlags?.isNotEmpty ?? false) + if (debuggingOptions.dartFlags.isNotEmpty) debuggingOptions.dartFlags, if (debuggingOptions.nullAssertions) '--null_assertions', diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart index f626c704d48..89866f21469 100644 --- a/packages/flutter_tools/lib/src/vmservice.dart +++ b/packages/flutter_tools/lib/src/vmservice.dart @@ -2,11 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; -import 'package:meta/meta.dart' show required, visibleForTesting; +import 'package:meta/meta.dart' show visibleForTesting; import 'package:vm_service/vm_service.dart' as vm_service; import 'base/common.dart'; @@ -31,7 +29,7 @@ const int kIsolateReloadBarred = 1005; /// Override `WebSocketConnector` in [context] to use a different constructor /// for [WebSocket]s (used by tests). -typedef WebSocketConnector = Future Function(String url, {io.CompressionOptions compression, @required Logger logger}); +typedef WebSocketConnector = Future Function(String url, {io.CompressionOptions compression, required Logger logger}); typedef PrintStructuredErrorLogMethod = void Function(vm_service.Event); @@ -41,7 +39,7 @@ WebSocketConnector _openChannel = _defaultOpenChannel; /// /// Provide a `null` value to restore the original connector. @visibleForTesting -set openChannelForTesting(WebSocketConnector connector) { +set openChannelForTesting(WebSocketConnector? connector) { _openChannel = connector ?? _defaultOpenChannel; } @@ -93,7 +91,7 @@ typedef CompileExpression = Future Function( List definitions, List typeDefinitions, String libraryUri, - String klass, + String? klass, bool isStatic, ); @@ -104,13 +102,13 @@ typedef GetSkSLMethod = Future Function(); Future _defaultOpenChannel(String url, { io.CompressionOptions compression = io.CompressionOptions.compressionDefault, - @required Logger logger, + required Logger logger, }) async { Duration delay = const Duration(milliseconds: 100); int attempts = 0; - io.WebSocket socket; + io.WebSocket? socket; - Future handleError(dynamic e) async { + Future handleError(Object? e) async { void Function(String) printVisibleTrace = logger.printTrace; if (attempts == 10) { logger.printStatus('Connecting to the VM Service is taking longer than expected...'); @@ -142,7 +140,7 @@ Future _defaultOpenChannel(String url, { final WebSocketConnector constructor = context.get() ?? (String url, { io.CompressionOptions compression = io.CompressionOptions.compressionDefault, - @required Logger logger, + Logger? logger, }) => io.WebSocket.connect(url, compression: compression); while (socket == null) { @@ -161,14 +159,14 @@ Future _defaultOpenChannel(String url, { /// Override `VMServiceConnector` in [context] to return a different VMService /// from [VMService.connect] (used by tests). typedef VMServiceConnector = Future Function(Uri httpUri, { - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - GetSkSLMethod getSkSLMethod, - PrintStructuredErrorLogMethod printStructuredErrorLogMethod, + ReloadSources? reloadSources, + Restart? restart, + CompileExpression? compileExpression, + GetSkSLMethod? getSkSLMethod, + PrintStructuredErrorLogMethod? printStructuredErrorLogMethod, io.CompressionOptions compression, - Device device, - @required Logger logger, + Device? device, + required Logger logger, }); /// Set up the VM Service client by attaching services for each of the provided @@ -176,12 +174,12 @@ typedef VMServiceConnector = Future Function(Uri httpUri, { /// /// All parameters besides [vmService] may be null. Future setUpVmService( - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - Device device, - GetSkSLMethod skSLMethod, - PrintStructuredErrorLogMethod printStructuredErrorLogMethod, + ReloadSources? reloadSources, + Restart? restart, + CompileExpression? compileExpression, + Device? device, + GetSkSLMethod? skSLMethod, + PrintStructuredErrorLogMethod? printStructuredErrorLogMethod, vm_service.VmService vmService ) async { // Each service registration requires a request to the attached VM service. Since the @@ -189,14 +187,14 @@ Future setUpVmService( // all at the end of this method. final List> registrationRequests = >[]; if (reloadSources != null) { - vmService.registerServiceCallback('reloadSources', (Map params) async { + vmService.registerServiceCallback('reloadSources', (Map params) async { final String isolateId = _validateRpcStringParam('reloadSources', params, 'isolateId'); final bool force = _validateRpcBoolParam('reloadSources', params, 'force'); final bool pause = _validateRpcBoolParam('reloadSources', params, 'pause'); await reloadSources(isolateId, force: force, pause: pause); - return { + return { 'result': { 'type': 'Success', } @@ -206,10 +204,10 @@ Future setUpVmService( } if (restart != null) { - vmService.registerServiceCallback('hotRestart', (Map params) async { + vmService.registerServiceCallback('hotRestart', (Map params) async { final bool pause = _validateRpcBoolParam('compileExpression', params, 'pause'); await restart(pause: pause); - return { + return { 'result': { 'type': 'Success', } @@ -218,12 +216,12 @@ Future setUpVmService( registrationRequests.add(vmService.registerService('hotRestart', 'Flutter Tools')); } - vmService.registerServiceCallback('flutterVersion', (Map params) async { + vmService.registerServiceCallback('flutterVersion', (Map params) async { final FlutterVersion version = context.get() ?? FlutterVersion(); final Map versionJson = version.toJson(); versionJson['frameworkRevisionShort'] = version.frameworkRevisionShort; versionJson['engineRevisionShort'] = version.engineRevisionShort; - return { + return { 'result': { 'type': 'Success', ...versionJson, @@ -233,29 +231,29 @@ Future setUpVmService( registrationRequests.add(vmService.registerService('flutterVersion', 'Flutter Tools')); if (compileExpression != null) { - vmService.registerServiceCallback('compileExpression', (Map params) async { + vmService.registerServiceCallback('compileExpression', (Map params) async { final String isolateId = _validateRpcStringParam('compileExpression', params, 'isolateId'); final String expression = _validateRpcStringParam('compileExpression', params, 'expression'); - final List definitions = List.from(params['definitions'] as List); - final List typeDefinitions = List.from(params['typeDefinitions'] as List); - final String libraryUri = params['libraryUri'] as String; - final String klass = params['klass'] as String; + final List definitions = List.from(params['definitions']! as List); + final List typeDefinitions = List.from(params['typeDefinitions']! as List); + final String libraryUri = params['libraryUri']! as String; + final String? klass = params['klass'] as String?; final bool isStatic = _validateRpcBoolParam('compileExpression', params, 'isStatic'); final String kernelBytesBase64 = await compileExpression(isolateId, expression, definitions, typeDefinitions, libraryUri, klass, isStatic); - return { + return { 'type': 'Success', - 'result': {'kernelBytes': kernelBytesBase64}, + 'result': {'kernelBytes': kernelBytesBase64}, }; }); registrationRequests.add(vmService.registerService('compileExpression', 'Flutter Tools')); } if (device != null) { - vmService.registerServiceCallback('flutterMemoryInfo', (Map params) async { + vmService.registerServiceCallback('flutterMemoryInfo', (Map params) async { final MemoryInfo result = await device.queryMemoryInfo(); - return { + return { 'result': { 'type': 'Success', ...result.toJson(), @@ -265,9 +263,9 @@ Future setUpVmService( registrationRequests.add(vmService.registerService('flutterMemoryInfo', 'Flutter Tools')); } if (skSLMethod != null) { - vmService.registerServiceCallback('flutterGetSkSL', (Map params) async { + vmService.registerServiceCallback('flutterGetSkSL', (Map params) async { final String filename = await skSLMethod(); - return { + return { 'result': { 'type': 'Success', 'filename': filename, @@ -282,7 +280,7 @@ Future setUpVmService( // thrown if we're already subscribed. registrationRequests.add(vmService .streamListen(vm_service.EventStreams.kExtension) - .catchError((dynamic error) {}, test: (dynamic error) => error is vm_service.RPCError) + .catchError((Object? error) {}, test: (Object? error) => error is vm_service.RPCError) ); } @@ -304,14 +302,14 @@ Future setUpVmService( /// See: https://github.com/dart-lang/sdk/commit/df8bf384eb815cf38450cb50a0f4b62230fba217 Future connectToVmService( Uri httpUri, { - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - GetSkSLMethod getSkSLMethod, - PrintStructuredErrorLogMethod printStructuredErrorLogMethod, + ReloadSources? reloadSources, + Restart? restart, + CompileExpression? compileExpression, + GetSkSLMethod? getSkSLMethod, + PrintStructuredErrorLogMethod? printStructuredErrorLogMethod, io.CompressionOptions compression = io.CompressionOptions.compressionDefault, - Device device, - @required Logger logger, + Device? device, + required Logger logger, }) async { final VMServiceConnector connector = context.get() ?? _connect; return connector(httpUri, @@ -329,7 +327,7 @@ Future connectToVmService( Future createVmServiceDelegate( Uri wsUri, { io.CompressionOptions compression = io.CompressionOptions.compressionDefault, - @required Logger logger, + required Logger logger, }) async { final io.WebSocket channel = await _openChannel(wsUri.toString(), compression: compression, logger: logger); return vm_service.VmService( @@ -344,14 +342,14 @@ Future createVmServiceDelegate( Future _connect( Uri httpUri, { - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - GetSkSLMethod getSkSLMethod, - PrintStructuredErrorLogMethod printStructuredErrorLogMethod, + ReloadSources? reloadSources, + Restart? restart, + CompileExpression? compileExpression, + GetSkSLMethod? getSkSLMethod, + PrintStructuredErrorLogMethod? printStructuredErrorLogMethod, io.CompressionOptions compression = io.CompressionOptions.compressionDefault, - Device device, - @required Logger logger, + Device? device, + required Logger logger, }) async { final Uri wsUri = httpUri.replace(scheme: 'ws', path: urlContext.join(httpUri.path, 'ws')); final vm_service.VmService delegateService = await createVmServiceDelegate( @@ -374,20 +372,20 @@ Future _connect( return FlutterVmService(service, httpAddress: httpUri, wsAddress: wsUri); } -String _validateRpcStringParam(String methodName, Map params, String paramName) { - final dynamic value = params[paramName]; - if (value is! String || (value as String).isEmpty) { +String _validateRpcStringParam(String methodName, Map params, String paramName) { + final Object? value = params[paramName]; + if (value is! String || value.isEmpty) { throw vm_service.RPCError( methodName, RPCErrorCodes.kInvalidParams, "Invalid '$paramName': $value", ); } - return value as String; + return value; } -bool _validateRpcBoolParam(String methodName, Map params, String paramName) { - final dynamic value = params[paramName]; +bool _validateRpcBoolParam(String methodName, Map params, String paramName) { + final Object? value = params[paramName]; if (value != null && value is! bool) { throw vm_service.RPCError( methodName, @@ -395,30 +393,30 @@ bool _validateRpcBoolParam(String methodName, Map params, Strin "Invalid '$paramName': $value", ); } - return (value as bool) ?? false; + return (value as bool?) ?? false; } /// Peered to an Android/iOS FlutterView widget on a device. class FlutterView { FlutterView({ - @required this.id, - @required this.uiIsolate, + required this.id, + required this.uiIsolate, }); factory FlutterView.parse(Map json) { - final Map rawIsolate = json['isolate'] as Map; - vm_service.IsolateRef isolate; + final Map? rawIsolate = json['isolate'] as Map?; + vm_service.IsolateRef? isolate; if (rawIsolate != null) { rawIsolate['number'] = rawIsolate['number']?.toString(); isolate = vm_service.IsolateRef.parse(rawIsolate); } return FlutterView( - id: json['id'] as String, + id: json['id']! as String, uiIsolate: isolate, ); } - final vm_service.IsolateRef uiIsolate; + final vm_service.IsolateRef? uiIsolate; final String id; bool get hasIsolate => uiIsolate != null; @@ -426,8 +424,8 @@ class FlutterView { @override String toString() => id; - Map toJson() { - return { + Map toJson() { + return { 'id': id, 'isolate': uiIsolate?.toJson(), }; @@ -436,16 +434,20 @@ class FlutterView { /// Flutter specific VM Service functionality. class FlutterVmService { - FlutterVmService(this.service, {this.wsAddress, this.httpAddress}); + FlutterVmService( + this.service, { + this.wsAddress, + this.httpAddress, + }); final vm_service.VmService service; - final Uri wsAddress; - final Uri httpAddress; + final Uri? wsAddress; + final Uri? httpAddress; - Future callMethodWrapper( + Future callMethodWrapper( String method, { - String isolateId, - Map args + String? isolateId, + Map? args }) async { try { return await service.callMethod(method, isolateId: isolateId, args: args); @@ -463,14 +465,13 @@ class FlutterVmService { /// Set the asset directory for the an attached Flutter view. Future setAssetDirectory({ - @required Uri assetsDirectory, - @required String viewId, - @required String uiIsolateId, + required Uri assetsDirectory, + required String? viewId, + required String? uiIsolateId, }) async { - assert(assetsDirectory != null); await callMethodWrapper(kSetAssetBundlePathMethod, isolateId: uiIsolateId, - args: { + args: { 'viewId': viewId, 'assetDirectory': assetsDirectory.toFilePath(windows: false), }); @@ -480,10 +481,10 @@ class FlutterVmService { /// /// This method will only return data if `--cache-sksl` was provided as a /// flutter run argument, and only then on physical devices. - Future> getSkSLs({ - @required String viewId, + Future?> getSkSLs({ + required String viewId, }) async { - final vm_service.Response response = await callMethodWrapper( + final vm_service.Response? response = await callMethodWrapper( kGetSkSLsMethod, args: { 'viewId': viewId, @@ -492,14 +493,14 @@ class FlutterVmService { if (response == null) { return null; } - return response.json['SkSLs'] as Map; + return response.json?['SkSLs'] as Map?; } /// Flush all tasks on the UI thread for an attached Flutter view. /// /// This method is currently used only for benchmarking. Future flushUIThreadTasks({ - @required String uiIsolateId, + required String uiIsolateId, }) async { await callMethodWrapper( kFlushUIThreadTasksMethod, @@ -515,9 +516,9 @@ class FlutterVmService { /// This method is used by the tool to hot restart an already running Flutter /// engine. Future runInView({ - @required String viewId, - @required Uri main, - @required Uri assetsDirectory, + required String viewId, + required Uri main, + required Uri assetsDirectory, }) async { try { await service.streamListen(vm_service.EventStreams.kIsolate); @@ -539,60 +540,63 @@ class FlutterVmService { } Future flutterDebugDumpApp({ - @required String isolateId, + required String isolateId, }) async { - final Map response = await invokeFlutterExtensionRpcRaw( + final Map? response = await invokeFlutterExtensionRpcRaw( 'ext.flutter.debugDumpApp', isolateId: isolateId, ); - return response != null ? response['data']?.toString() : ''; + return response?['data']?.toString() ?? ''; } Future flutterDebugDumpRenderTree({ - @required String isolateId, + required String isolateId, }) async { - final Map response = await invokeFlutterExtensionRpcRaw( + final Map? response = await invokeFlutterExtensionRpcRaw( 'ext.flutter.debugDumpRenderTree', isolateId: isolateId, args: {} ); - return response != null ? response['data']?.toString() : ''; + return response?['data']?.toString() ?? ''; } Future flutterDebugDumpLayerTree({ - @required String isolateId, + required String isolateId, }) async { - final Map response = await invokeFlutterExtensionRpcRaw( + final Map? response = await invokeFlutterExtensionRpcRaw( 'ext.flutter.debugDumpLayerTree', isolateId: isolateId, ); - return response != null ? response['data']?.toString() : ''; + return response?['data']?.toString() ?? ''; } Future flutterDebugDumpSemanticsTreeInTraversalOrder({ - @required String isolateId, + required String isolateId, }) async { - final Map response = await invokeFlutterExtensionRpcRaw( + final Map? response = await invokeFlutterExtensionRpcRaw( 'ext.flutter.debugDumpSemanticsTreeInTraversalOrder', isolateId: isolateId, ); - return response != null ? response['data']?.toString() : ''; + return response?['data']?.toString() ?? ''; } Future flutterDebugDumpSemanticsTreeInInverseHitTestOrder({ - @required String isolateId, + required String isolateId, }) async { - final Map response = await invokeFlutterExtensionRpcRaw( + final Map? response = await invokeFlutterExtensionRpcRaw( 'ext.flutter.debugDumpSemanticsTreeInInverseHitTestOrder', isolateId: isolateId, ); - return response != null ? response['data']?.toString() : ''; + if (response != null) { + return response['data']?.toString() ?? ''; + } + return ''; } - Future> _flutterToggle(String name, { - @required String isolateId, + Future?> _flutterToggle(String name, { + required String isolateId, }) async { - Map state = await invokeFlutterExtensionRpcRaw( + Map? state = await invokeFlutterExtensionRpcRaw( 'ext.flutter.$name', isolateId: isolateId, ); @@ -600,7 +604,7 @@ class FlutterVmService { state = await invokeFlutterExtensionRpcRaw( 'ext.flutter.$name', isolateId: isolateId, - args: { + args: { 'enabled': state['enabled'] == 'true' ? 'false' : 'true', }, ); @@ -609,38 +613,38 @@ class FlutterVmService { return state; } - Future> flutterToggleDebugPaintSizeEnabled({ - @required String isolateId, + Future?> flutterToggleDebugPaintSizeEnabled({ + required String isolateId, }) => _flutterToggle('debugPaint', isolateId: isolateId); - Future> flutterTogglePerformanceOverlayOverride({ - @required String isolateId, + Future?> flutterTogglePerformanceOverlayOverride({ + required String isolateId, }) => _flutterToggle('showPerformanceOverlay', isolateId: isolateId); - Future> flutterToggleWidgetInspector({ - @required String isolateId, + Future?> flutterToggleWidgetInspector({ + required String isolateId, }) => _flutterToggle('inspector.show', isolateId: isolateId); - Future> flutterToggleInvertOversizedImages({ - @required String isolateId, + Future?> flutterToggleInvertOversizedImages({ + required String isolateId, }) => _flutterToggle('invertOversizedImages', isolateId: isolateId); - Future> flutterToggleProfileWidgetBuilds({ - @required String isolateId, + Future?> flutterToggleProfileWidgetBuilds({ + required String isolateId, }) => _flutterToggle('profileWidgetBuilds', isolateId: isolateId); - Future> flutterDebugAllowBanner(bool show, { - @required String isolateId, + Future?> flutterDebugAllowBanner(bool show, { + required String isolateId, }) { return invokeFlutterExtensionRpcRaw( 'ext.flutter.debugAllowBanner', isolateId: isolateId, - args: {'enabled': show ? 'true' : 'false'}, + args: {'enabled': show ? 'true' : 'false'}, ); } - Future> flutterReassemble({ - @required String isolateId, + Future?> flutterReassemble({ + required String isolateId, }) { return invokeFlutterExtensionRpcRaw( 'ext.flutter.reassemble', @@ -648,9 +652,9 @@ class FlutterVmService { ); } - Future> flutterFastReassemble({ - @required String isolateId, - @required String className, + Future?> flutterFastReassemble({ + required String isolateId, + required String className, }) { return invokeFlutterExtensionRpcRaw( 'ext.flutter.fastReassemble', @@ -662,18 +666,18 @@ class FlutterVmService { } Future flutterAlreadyPaintedFirstUsefulFrame({ - @required String isolateId, + required String isolateId, }) async { - final Map result = await invokeFlutterExtensionRpcRaw( + final Map? result = await invokeFlutterExtensionRpcRaw( 'ext.flutter.didSendFirstFrameRasterizedEvent', isolateId: isolateId, ); // result might be null when the service extension is not initialized - return result != null && result['enabled'] == 'true'; + return result?['enabled'] == 'true'; } - Future> uiWindowScheduleFrame({ - @required String isolateId, + Future?> uiWindowScheduleFrame({ + required String isolateId, }) { return invokeFlutterExtensionRpcRaw( 'ext.ui.window.scheduleFrame', @@ -681,13 +685,13 @@ class FlutterVmService { ); } - Future> flutterEvictAsset(String assetPath, { - @required String isolateId, + Future?> flutterEvictAsset(String assetPath, { + required String isolateId, }) { return invokeFlutterExtensionRpcRaw( 'ext.flutter.evict', isolateId: isolateId, - args: { + args: { 'value': assetPath, }, ); @@ -698,10 +702,10 @@ class FlutterVmService { /// This method is only supported by certain embedders. This is /// described by [Device.supportsFlutterExit]. Future flutterExit({ - @required String isolateId, + required String isolateId, }) async { try { - final Map result = await invokeFlutterExtensionRpcRaw( + final Map? result = await invokeFlutterExtensionRpcRaw( 'ext.flutter.exit', isolateId: isolateId, ); @@ -725,18 +729,18 @@ class FlutterVmService { /// If a non-null value is provided for [platform], the platform override /// is updated with this value. Future flutterPlatformOverride({ - String platform, - @required String isolateId, + String? platform, + required String isolateId, }) async { - final Map result = await invokeFlutterExtensionRpcRaw( + final Map? result = await invokeFlutterExtensionRpcRaw( 'ext.flutter.platformOverride', isolateId: isolateId, args: platform != null - ? {'value': platform} + ? {'value': platform} : {}, ); if (result != null && result['value'] is String) { - return result['value'] as String; + return result['value']! as String; } return 'unknown'; } @@ -746,15 +750,15 @@ class FlutterVmService { /// /// If a non-null value is provided for [brightness], the brightness override /// is updated with this value. - Future flutterBrightnessOverride({ - Brightness brightness, - @required String isolateId, + Future flutterBrightnessOverride({ + Brightness? brightness, + required String isolateId, }) async { - final Map result = await invokeFlutterExtensionRpcRaw( + final Map? result = await invokeFlutterExtensionRpcRaw( 'ext.flutter.brightnessOverride', isolateId: isolateId, args: brightness != null - ? {'value': brightness.toString()} + ? {'value': brightness.toString()} : {}, ); if (result != null && result['value'] is String) { @@ -765,9 +769,9 @@ class FlutterVmService { return null; } - Future _checkedCallServiceExtension( + Future _checkedCallServiceExtension( String method, { - Map args, + Map? args, }) async { try { return await service.callServiceExtension(method, args: args); @@ -784,14 +788,14 @@ class FlutterVmService { /// Invoke a flutter extension method, if the flutter extension is not /// available, returns null. - Future> invokeFlutterExtensionRpcRaw( + Future?> invokeFlutterExtensionRpcRaw( String method, { - @required String isolateId, - Map args, + required String isolateId, + Map? args, }) async { - final vm_service.Response response = await _checkedCallServiceExtension( + final vm_service.Response? response = await _checkedCallServiceExtension( method, - args: { + args: { 'isolateId': isolateId, ...?args, }, @@ -810,7 +814,7 @@ class FlutterVmService { Duration delay = const Duration(milliseconds: 50), }) async { while (true) { - final vm_service.Response response = await callMethodWrapper( + final vm_service.Response? response = await callMethodWrapper( kListViewsMethod, ); if (response == null) { @@ -819,7 +823,7 @@ class FlutterVmService { // with cleaning up. return []; } - final List rawViews = response.json['views'] as List; + final List? rawViews = response.json?['views'] as List?; final List views = [ if (rawViews != null) for (final Object rawView in rawViews) @@ -850,11 +854,11 @@ class FlutterVmService { } final Completer extensionAdded = Completer(); - StreamSubscription isolateEvents; + late final StreamSubscription isolateEvents; isolateEvents = service.onIsolateEvent.listen((vm_service.Event event) { if (event.kind == vm_service.EventKind.kServiceExtensionAdded && event.extensionRPC == extensionName) { - isolateEvents?.cancel(); + isolateEvents.cancel(); extensionAdded.complete(event.isolate); } }); @@ -862,7 +866,7 @@ class FlutterVmService { try { final List refs = await _getIsolateRefs(); for (final vm_service.IsolateRef ref in refs) { - final vm_service.Isolate isolate = await getIsolateOrNull(ref.id); + final vm_service.Isolate? isolate = await getIsolateOrNull(ref.id!); if (isolate != null && isolate.extensionRPCs?.contains(extensionName) == true) { return ref; } @@ -886,7 +890,7 @@ class FlutterVmService { final List refs = []; for (final FlutterView flutterView in flutterViews) { - final vm_service.IsolateRef uiIsolate = flutterView.uiIsolate; + final vm_service.IsolateRef? uiIsolate = flutterView.uiIsolate; if (uiIsolate != null) { refs.add(uiIsolate); } @@ -896,11 +900,13 @@ class FlutterVmService { /// Attempt to retrieve the isolate with id [isolateId], or `null` if it has /// been collected. - Future getIsolateOrNull(String isolateId) async { + Future getIsolateOrNull(String isolateId) async { return service.getIsolate(isolateId) - .catchError((dynamic error, StackTrace stackTrace) { + // The .then() call is required to cast from Future to Future + .then((vm_service.Isolate isolate) => isolate) + .catchError((Object? error, StackTrace stackTrace) { return null; - }, test: (dynamic error) { + }, test: (Object? error) { return (error is vm_service.SentinelException) || (error is vm_service.RPCError && error.code == RPCErrorCodes.kServiceDisappeared); }); @@ -912,7 +918,7 @@ class FlutterVmService { // has custom handling of certain RPCErrors. return service.callServiceExtension( '_createDevFS', - args: {'fsName': fsName}, + args: {'fsName': fsName}, ); } @@ -920,15 +926,15 @@ class FlutterVmService { Future deleteDevFS(String fsName) async { await _checkedCallServiceExtension( '_deleteDevFS', - args: {'fsName': fsName}, + args: {'fsName': fsName}, ); } - Future screenshot() { + Future screenshot() { return _checkedCallServiceExtension(kScreenshotMethod); } - Future screenshotSkp() { + Future screenshotSkp() { return _checkedCallServiceExtension(kScreenshotSkpMethod); } @@ -937,13 +943,13 @@ class FlutterVmService { assert(recordedStreams != null); await _checkedCallServiceExtension( 'setVMTimelineFlags', - args: { + args: { 'recordedStreams': recordedStreams, }, ); } - Future getTimeline() { + Future getTimeline() { return _checkedCallServiceExtension('getVMTimeline'); } @@ -985,7 +991,7 @@ enum Brightness { /// Process a VM service log event into a string message. String processVmServiceMessage(vm_service.Event event) { - final String message = utf8.decode(base64.decode(event.bytes)); + final String message = utf8.decode(base64.decode(event.bytes!)); // Remove extra trailing newlines appended by the vm service. if (message.endsWith('\n')) { return message.substring(0, message.length - 1); diff --git a/packages/flutter_tools/test/general.shard/device_test.dart b/packages/flutter_tools/test/general.shard/device_test.dart index b2958fa6604..515db621d4e 100644 --- a/packages/flutter_tools/test/general.shard/device_test.dart +++ b/packages/flutter_tools/test/general.shard/device_test.dart @@ -481,7 +481,7 @@ void main() { }); testWithoutContext('computeDartVmFlags handles various combinations of Dart VM flags and null_assertions', () { - expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, dartFlags: null)), ''); + expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug)), ''); expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, dartFlags: '--foo')), '--foo'); expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, dartFlags: '', nullAssertions: true)), '--null_assertions'); expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, dartFlags: '--foo', nullAssertions: true)), '--foo,--null_assertions'); diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index dda956265b8..7eb912f232e 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -796,7 +796,7 @@ void main() { }, ), ]); - final FakeDelegateFlutterDevice flutterDevice = FakeDelegateFlutterDevice( + final FakeDelegateFlutterDevice flutterDevice = FakeDelegateFlutterDevice( device, BuildInfo.debug, FakeResidentCompiler(), diff --git a/packages/flutter_tools/test/src/fake_vm_services.dart b/packages/flutter_tools/test/src/fake_vm_services.dart index 299f3e18a18..b84ecf1d8ac 100644 --- a/packages/flutter_tools/test/src/fake_vm_services.dart +++ b/packages/flutter_tools/test/src/fake_vm_services.dart @@ -2,14 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/convert.dart'; import 'package:flutter_tools/src/vmservice.dart'; -import 'package:meta/meta.dart'; import 'package:test_api/test_api.dart' hide test; // ignore: deprecated_member_use import 'package:vm_service/vm_service.dart' as vm_service; @@ -19,9 +16,9 @@ export 'package:test_api/test_api.dart' hide test, isInstanceOf; // ignore: depr /// and response structure. class FakeVmServiceHost { FakeVmServiceHost({ - @required List requests, - Uri httpAddress, - Uri wsAddress, + required List requests, + Uri? httpAddress, + Uri? wsAddress, }) : _requests = requests { _vmService = FlutterVmService(vm_service.VmService( _input.stream, @@ -44,16 +41,16 @@ class FakeVmServiceHost { return; } if (fakeRequest.errorCode == null) { - _input.add(json.encode({ + _input.add(json.encode({ 'jsonrpc': '2.0', 'id': request['id'], 'result': fakeRequest.jsonResponse ?? {'type': 'Success'}, })); } else { - _input.add(json.encode({ + _input.add(json.encode({ 'jsonrpc': '2.0', 'id': request['id'], - 'error': { + 'error': { 'code': fakeRequest.errorCode, } })); @@ -67,7 +64,7 @@ class FakeVmServiceHost { final StreamController _output = StreamController(); FlutterVmService get vmService => _vmService; - FlutterVmService _vmService; + late final FlutterVmService _vmService; bool get hasRemainingExpectations => _requests.isNotEmpty; @@ -95,7 +92,7 @@ abstract class VmServiceExpectation { class FakeVmServiceRequest implements VmServiceExpectation { const FakeVmServiceRequest({ - @required this.method, + required this.method, this.args = const {}, this.jsonResponse, this.errorCode, @@ -109,9 +106,9 @@ class FakeVmServiceRequest implements VmServiceExpectation { /// If non-null, the error code for a [vm_service.RPCError] in place of a /// standard response. - final int errorCode; - final Map args; - final Map jsonResponse; + final int? errorCode; + final Map? args; + final Map? jsonResponse; @override bool get isRequest => true; @@ -119,8 +116,8 @@ class FakeVmServiceRequest implements VmServiceExpectation { class FakeVmServiceStreamResponse implements VmServiceExpectation { const FakeVmServiceStreamResponse({ - @required this.event, - @required this.streamId, + required this.event, + required this.streamId, }); final vm_service.Event event;