From 7884420a0a32467fa268761b99cf19c58480a499 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Fri, 31 Jul 2020 20:20:00 +0200 Subject: [PATCH] migrate services to nullsafety (#62513) NNBD migration for services --- dev/tools/gen_keycodes/data/keyboard_key.tmpl | 16 +- packages/flutter/lib/services.dart | 2 - .../lib/src/services/asset_bundle.dart | 11 +- .../flutter/lib/src/services/autofill.dart | 13 +- .../lib/src/services/binary_messenger.dart | 13 +- .../flutter/lib/src/services/binding.dart | 47 +++--- .../flutter/lib/src/services/clipboard.dart | 9 +- .../flutter/lib/src/services/font_loader.dart | 3 +- .../lib/src/services/haptic_feedback.dart | 1 - .../lib/src/services/keyboard_key.dart | 17 +- .../lib/src/services/keyboard_maps.dart | 1 - .../lib/src/services/message_codec.dart | 15 +- .../lib/src/services/message_codecs.dart | 43 +++-- .../lib/src/services/platform_channel.dart | 57 ++++--- .../lib/src/services/platform_messages.dart | 7 +- .../lib/src/services/platform_views.dart | 157 ++++++++---------- .../lib/src/services/raw_keyboard.dart | 82 +++++---- .../src/services/raw_keyboard_android.dart | 22 +-- .../src/services/raw_keyboard_fuchsia.dart | 14 +- .../lib/src/services/raw_keyboard_linux.dart | 43 ++--- .../lib/src/services/raw_keyboard_macos.dart | 19 +-- .../lib/src/services/raw_keyboard_web.dart | 9 +- .../src/services/raw_keyboard_windows.dart | 19 +-- .../flutter/lib/src/services/restoration.dart | 71 ++++---- .../lib/src/services/system_channels.dart | 3 +- .../lib/src/services/system_chrome.dart | 37 ++--- .../lib/src/services/system_navigator.dart | 3 +- .../lib/src/services/system_sound.dart | 1 - .../lib/src/services/text_editing.dart | 15 +- .../lib/src/services/text_formatter.dart | 11 +- .../flutter/lib/src/services/text_input.dart | 81 +++++---- 31 files changed, 388 insertions(+), 454 deletions(-) diff --git a/dev/tools/gen_keycodes/data/keyboard_key.tmpl b/dev/tools/gen_keycodes/data/keyboard_key.tmpl index 559d3a12cb..80cf3b4d00 100644 --- a/dev/tools/gen_keycodes/data/keyboard_key.tmpl +++ b/dev/tools/gen_keycodes/data/keyboard_key.tmpl @@ -149,13 +149,15 @@ class LogicalKeyboardKey extends KeyboardKey { /// The debug string to print for this keyboard key, which will be null in /// release mode. - final String debugName; + final String? debugName; /// The Unicode string representing the character produced by a [RawKeyEvent]. /// /// This value is useful for describing or matching mnemonic keyboard /// shortcuts. /// + /// This value can be null if there's no key label data for a key. + /// /// On most platforms this is a single code point, but it could contain any /// Unicode string. The `keyLabel` differs from [RawKeyEvent.character] /// because `keyLabel` only takes into account the key being pressed, not any @@ -164,7 +166,7 @@ class LogicalKeyboardKey extends KeyboardKey { /// “o” for [keyLabel], but would return “ö” for [RawKeyEvent.character]. /// /// {@macro flutter.services.RawKeyEventData.keyLabel} - final String keyLabel; + final String? keyLabel; @override int get hashCode => keyId.hashCode; @@ -180,7 +182,7 @@ class LogicalKeyboardKey extends KeyboardKey { /// Returns the [LogicalKeyboardKey] constant that matches the given ID, or /// null, if not found. - static LogicalKeyboardKey findKeyByKeyId(int keyId) => _knownLogicalKeys[keyId]; + static LogicalKeyboardKey? findKeyByKeyId(int keyId) => _knownLogicalKeys[keyId]; /// Returns true if the given label represents a Unicode control character. /// @@ -237,7 +239,7 @@ class LogicalKeyboardKey extends KeyboardKey { /// this is a [shiftLeft] key, this accessor will return the set /// `{ shift }`. Set get synonyms { - final LogicalKeyboardKey result = _synonyms[this]; + final LogicalKeyboardKey? result = _synonyms[this]; return result == null ? {} : {result}; } @@ -250,7 +252,7 @@ class LogicalKeyboardKey extends KeyboardKey { static Set collapseSynonyms(Set input) { final Set result = {}; for (final LogicalKeyboardKey key in input) { - final LogicalKeyboardKey synonym = _synonyms[key]; + final LogicalKeyboardKey? synonym = _synonyms[key]; result.add(synonym ?? key); } return result; @@ -434,11 +436,11 @@ class PhysicalKeyboardKey extends KeyboardKey { /// The debug string to print for this keyboard key, which will be null in /// release mode. - final String debugName; + final String? debugName; /// Finds a known [PhysicalKeyboardKey] that matches the given USB HID usage /// code. - static PhysicalKeyboardKey findKeyByCode(int usageCode) => _knownPhysicalKeys[usageCode]; + static PhysicalKeyboardKey? findKeyByCode(int usageCode) => _knownPhysicalKeys[usageCode]; @override int get hashCode => usbHidUsage.hashCode; diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index b794ac309a..443adc13a6 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.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 - /// Platform services exposed to Flutter apps. /// /// To use, import `package:flutter/services.dart`. diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index 461f886124..6c66a6c673 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -2,7 +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:convert'; @@ -181,11 +180,11 @@ abstract class CachingAssetBundle extends AssetBundle { assert(parser != null); if (_structuredDataCache.containsKey(key)) return _structuredDataCache[key] as Future; - Completer completer; - Future result; + Completer? completer; + Future? result; loadString(key, cache: false).then(parser).then((T value) { result = SynchronousFuture(value); - _structuredDataCache[key] = result; + _structuredDataCache[key] = result!; if (completer != null) { // We already returned from the loadStructuredData function, which means // we are in the asynchronous mode. Pass the value to the completer. The @@ -196,7 +195,7 @@ abstract class CachingAssetBundle extends AssetBundle { if (result != null) { // The code above ran synchronously, and came up with an answer. // Return the SynchronousFuture that we created above. - return result; + return result!; } // The code above hasn't yet run its "then" handler yet. Let's prepare a // completer for it to use when it does run. @@ -217,7 +216,7 @@ class PlatformAssetBundle extends CachingAssetBundle { @override Future load(String key) async { final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path); - final ByteData asset = + final ByteData? asset = await defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData()); if (asset == null) throw FlutterError('Unable to load asset: $key'); diff --git a/packages/flutter/lib/src/services/autofill.dart b/packages/flutter/lib/src/services/autofill.dart index c3ad055368..2ecec8f5a4 100644 --- a/packages/flutter/lib/src/services/autofill.dart +++ b/packages/flutter/lib/src/services/autofill.dart @@ -2,7 +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:flutter/foundation.dart'; import 'text_input.dart'; @@ -629,9 +628,9 @@ class AutofillConfiguration { /// Creates autofill related configuration information that can be sent to the /// platform. const AutofillConfiguration({ - @required this.uniqueIdentifier, - @required this.autofillHints, - this.currentEditingValue, + required this.uniqueIdentifier, + required this.autofillHints, + required this.currentEditingValue, }) : assert(uniqueIdentifier != null), assert(autofillHints != null); @@ -749,7 +748,7 @@ abstract class AutofillScope { /// this [AutofillScope]. /// /// Returns null if there's no matching [AutofillClient]. - AutofillClient getAutofillClient(String autofillId); + AutofillClient? getAutofillClient(String autofillId); /// The collection of [AutofillClient]s currently tied to this [AutofillScope]. /// @@ -767,8 +766,8 @@ abstract class AutofillScope { @immutable class _AutofillScopeTextInputConfiguration extends TextInputConfiguration { _AutofillScopeTextInputConfiguration({ - @required this.allConfigurations, - @required TextInputConfiguration currentClientConfiguration, + required this.allConfigurations, + required TextInputConfiguration currentClientConfiguration, }) : assert(allConfigurations != null), assert(currentClientConfiguration != null), super(inputType: currentClientConfiguration.inputType, diff --git a/packages/flutter/lib/src/services/binary_messenger.dart b/packages/flutter/lib/src/services/binary_messenger.dart index 789f586178..6f4e613521 100644 --- a/packages/flutter/lib/src/services/binary_messenger.dart +++ b/packages/flutter/lib/src/services/binary_messenger.dart @@ -2,7 +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:typed_data'; @@ -12,7 +11,7 @@ import 'package:flutter/foundation.dart'; import 'binding.dart'; /// A function which takes a platform message and asynchronously returns an encoded response. -typedef MessageHandler = Future Function(ByteData message); +typedef MessageHandler = Future Function(ByteData? message); /// A messenger which sends binary data across the Flutter platform barrier. /// @@ -27,13 +26,13 @@ abstract class BinaryMessenger { /// from [Window.onPlatformMessage]. /// /// To register a handler for a given message channel, see [setMessageHandler]. - Future handlePlatformMessage(String channel, ByteData data, ui.PlatformMessageResponseCallback callback); + Future handlePlatformMessage(String channel, ByteData? data, ui.PlatformMessageResponseCallback? callback); /// Send a binary message to the platform plugins on the given channel. /// /// Returns a [Future] which completes to the received response, undecoded, /// in binary form. - Future send(String channel, ByteData message); + Future send(String channel, ByteData? message); /// Set a callback for receiving messages from the platform plugins on the /// given channel, without decoding them. @@ -43,7 +42,7 @@ abstract class BinaryMessenger { /// argument. /// /// The handler's return value, if non-null, is sent as a response, unencoded. - void setMessageHandler(String channel, MessageHandler handler); + void setMessageHandler(String channel, MessageHandler? handler); /// Returns true if the `handler` argument matches the `handler` previously /// passed to [setMessageHandler]. @@ -63,7 +62,7 @@ abstract class BinaryMessenger { /// /// This is intended for testing. Messages intercepted in this manner are not /// sent to platform plugins. - void setMockMessageHandler(String channel, MessageHandler handler); + void setMockMessageHandler(String channel, MessageHandler? handler); /// Returns true if the `handler` argument matches the `handler` previously /// passed to [setMockMessageHandler]. @@ -104,5 +103,5 @@ BinaryMessenger get defaultBinaryMessenger { } return true; }()); - return ServicesBinding.instance.defaultBinaryMessenger; + return ServicesBinding.instance!.defaultBinaryMessenger; } diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index 5937bdad77..6f7e769a4e 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -2,7 +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:typed_data'; @@ -31,14 +30,14 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { _restorationManager = createRestorationManager(); window.onPlatformMessage = defaultBinaryMessenger.handlePlatformMessage; initLicenses(); - SystemChannels.system.setMessageHandler(handleSystemMessage); + SystemChannels.system.setMessageHandler((dynamic message) => handleSystemMessage(message as Object)); SystemChannels.lifecycle.setMessageHandler(_handleLifecycleMessage); readInitialLifecycleStateFromNativeWindow(); } /// The current [ServicesBinding], if one has been created. - static ServicesBinding get instance => _instance; - static ServicesBinding _instance; + static ServicesBinding? get instance => _instance; + static ServicesBinding? _instance; /// The default instance of [BinaryMessenger]. /// @@ -46,7 +45,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { /// keeps track of which handlers have been registered on each channel so /// it may dispatch incoming messages to the registered handler. BinaryMessenger get defaultBinaryMessenger => _defaultBinaryMessenger; - BinaryMessenger _defaultBinaryMessenger; + late BinaryMessenger _defaultBinaryMessenger; /// Creates a default [BinaryMessenger] instance that can be used for sending /// platform messages. @@ -110,7 +109,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { await rawLicenses.future; final Completer> parsedLicenses = Completer>(); scheduleTask(() async { - parsedLicenses.complete(compute(_parseLicenses, await rawLicenses.future, debugLabel: 'parseLicenses')); + parsedLicenses.complete(compute>(_parseLicenses, await rawLicenses.future, debugLabel: 'parseLicenses')); }, Priority.animation); await parsedLicenses.future; yield* Stream.fromIterable(await parsedLicenses.future); @@ -183,18 +182,18 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { if (lifecycleState != null) { return; } - final AppLifecycleState state = _parseAppLifecycleMessage(window.initialLifecycleState); + final AppLifecycleState? state = _parseAppLifecycleMessage(window.initialLifecycleState); if (state != null) { handleAppLifecycleStateChanged(state); } } - Future _handleLifecycleMessage(String message) async { - handleAppLifecycleStateChanged(_parseAppLifecycleMessage(message)); + Future _handleLifecycleMessage(String? message) async { + handleAppLifecycleStateChanged(_parseAppLifecycleMessage(message!)!); return null; } - static AppLifecycleState _parseAppLifecycleMessage(String message) { + static AppLifecycleState? _parseAppLifecycleMessage(String message) { switch (message) { case 'AppLifecycleState.paused': return AppLifecycleState.paused; @@ -218,7 +217,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { /// [createRestorationManager], which is called to create the instance /// returned by this getter. RestorationManager get restorationManager => _restorationManager; - RestorationManager _restorationManager; + late RestorationManager _restorationManager; /// Creates the [RestorationManager] instance available via /// [restorationManager]. @@ -248,15 +247,15 @@ class _DefaultBinaryMessenger extends BinaryMessenger { static final Map _mockHandlers = {}; - Future _sendPlatformMessage(String channel, ByteData message) { - final Completer completer = Completer(); + Future _sendPlatformMessage(String channel, ByteData? message) { + final Completer completer = Completer(); // ui.window is accessed directly instead of using ServicesBinding.instance.window // because this method might be invoked before any binding is initialized. // This issue was reported in #27541. It is not ideal to statically access // ui.window because the Window may be dependency injected elsewhere with // a different instance. However, static access at this location seems to be // the least bad option. - ui.window.sendPlatformMessage(channel, message, (ByteData reply) { + ui.window.sendPlatformMessage(channel, message, (ByteData? reply) { try { completer.complete(reply); } catch (exception, stack) { @@ -274,16 +273,16 @@ class _DefaultBinaryMessenger extends BinaryMessenger { @override Future handlePlatformMessage( String channel, - ByteData data, - ui.PlatformMessageResponseCallback callback, + ByteData? data, + ui.PlatformMessageResponseCallback? callback, ) async { - ByteData response; + ByteData? response; try { - final MessageHandler handler = _handlers[channel]; + final MessageHandler? handler = _handlers[channel]; if (handler != null) { response = await handler(data); } else { - ui.channelBuffers.push(channel, data, callback); + ui.channelBuffers.push(channel, data, callback!); callback = null; } } catch (exception, stack) { @@ -301,20 +300,20 @@ class _DefaultBinaryMessenger extends BinaryMessenger { } @override - Future send(String channel, ByteData message) { - final MessageHandler handler = _mockHandlers[channel]; + Future send(String channel, ByteData? message) { + final MessageHandler? handler = _mockHandlers[channel]; if (handler != null) return handler(message); return _sendPlatformMessage(channel, message); } @override - void setMessageHandler(String channel, MessageHandler handler) { + void setMessageHandler(String channel, MessageHandler? handler) { if (handler == null) _handlers.remove(channel); else _handlers[channel] = handler; - ui.channelBuffers.drain(channel, (ByteData data, ui.PlatformMessageResponseCallback callback) async { + ui.channelBuffers.drain(channel, (ByteData? data, ui.PlatformMessageResponseCallback callback) async { await handlePlatformMessage(channel, data, callback); }); } @@ -323,7 +322,7 @@ class _DefaultBinaryMessenger extends BinaryMessenger { bool checkMessageHandler(String channel, MessageHandler handler) => _handlers[channel] == handler; @override - void setMockMessageHandler(String channel, MessageHandler handler) { + void setMockMessageHandler(String channel, MessageHandler? handler) { if (handler == null) _mockHandlers.remove(channel); else diff --git a/packages/flutter/lib/src/services/clipboard.dart b/packages/flutter/lib/src/services/clipboard.dart index 6560e680d3..c7478091db 100644 --- a/packages/flutter/lib/src/services/clipboard.dart +++ b/packages/flutter/lib/src/services/clipboard.dart @@ -2,7 +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'; @@ -20,7 +19,7 @@ class ClipboardData { const ClipboardData({ this.text }); /// Plain text variant of this clipboard data. - final String text; + final String? text; } /// Utility methods for interacting with the system's clipboard. @@ -54,13 +53,13 @@ class Clipboard { /// /// Returns a future which completes to null if the data could not be /// obtained, and to a [ClipboardData] object if it could. - static Future getData(String format) async { - final Map result = await SystemChannels.platform.invokeMethod( + static Future getData(String format) async { + final Map? result = await SystemChannels.platform.invokeMethod( 'Clipboard.getData', format, ); if (result == null) return null; - return ClipboardData(text: result['text'] as String); + return ClipboardData(text: result['text'] as String?); } } diff --git a/packages/flutter/lib/src/services/font_loader.dart b/packages/flutter/lib/src/services/font_loader.dart index 549a91a1da..e69fa0c2f5 100644 --- a/packages/flutter/lib/src/services/font_loader.dart +++ b/packages/flutter/lib/src/services/font_loader.dart @@ -2,7 +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:typed_data'; @@ -64,7 +63,7 @@ class FontLoader { (Uint8List list) => loadFont(list, family) ) ); - return Future.wait(loadFutures.toList()); + await Future.wait(loadFutures.toList()); } /// Hook called to load a font asset into the engine. diff --git a/packages/flutter/lib/src/services/haptic_feedback.dart b/packages/flutter/lib/src/services/haptic_feedback.dart index ae8c5c4458..6f0fbf085e 100644 --- a/packages/flutter/lib/src/services/haptic_feedback.dart +++ b/packages/flutter/lib/src/services/haptic_feedback.dart @@ -2,7 +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'; diff --git a/packages/flutter/lib/src/services/keyboard_key.dart b/packages/flutter/lib/src/services/keyboard_key.dart index 860198beec..63e560f8d6 100644 --- a/packages/flutter/lib/src/services/keyboard_key.dart +++ b/packages/flutter/lib/src/services/keyboard_key.dart @@ -2,7 +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 // DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT // This file is generated by dev/tools/gen_keycodes/bin/gen_keycodes.dart and @@ -149,13 +148,15 @@ class LogicalKeyboardKey extends KeyboardKey { /// The debug string to print for this keyboard key, which will be null in /// release mode. - final String debugName; + final String? debugName; /// The Unicode string representing the character produced by a [RawKeyEvent]. /// /// This value is useful for describing or matching mnemonic keyboard /// shortcuts. /// + /// This value can be null if there's no key label data for a key. + /// /// On most platforms this is a single code point, but it could contain any /// Unicode string. The `keyLabel` differs from [RawKeyEvent.character] /// because `keyLabel` only takes into account the key being pressed, not any @@ -164,7 +165,7 @@ class LogicalKeyboardKey extends KeyboardKey { /// “o” for [keyLabel], but would return “ö” for [RawKeyEvent.character]. /// /// {@macro flutter.services.RawKeyEventData.keyLabel} - final String keyLabel; + final String? keyLabel; @override int get hashCode => keyId.hashCode; @@ -180,7 +181,7 @@ class LogicalKeyboardKey extends KeyboardKey { /// Returns the [LogicalKeyboardKey] constant that matches the given ID, or /// null, if not found. - static LogicalKeyboardKey findKeyByKeyId(int keyId) => _knownLogicalKeys[keyId]; + static LogicalKeyboardKey? findKeyByKeyId(int keyId) => _knownLogicalKeys[keyId]; /// Returns true if the given label represents a Unicode control character. /// @@ -237,7 +238,7 @@ class LogicalKeyboardKey extends KeyboardKey { /// this is a [shiftLeft] key, this accessor will return the set /// `{ shift }`. Set get synonyms { - final LogicalKeyboardKey result = _synonyms[this]; + final LogicalKeyboardKey? result = _synonyms[this]; return result == null ? {} : {result}; } @@ -250,7 +251,7 @@ class LogicalKeyboardKey extends KeyboardKey { static Set collapseSynonyms(Set input) { final Set result = {}; for (final LogicalKeyboardKey key in input) { - final LogicalKeyboardKey synonym = _synonyms[key]; + final LogicalKeyboardKey? synonym = _synonyms[key]; result.add(synonym ?? key); } return result; @@ -2087,11 +2088,11 @@ class PhysicalKeyboardKey extends KeyboardKey { /// The debug string to print for this keyboard key, which will be null in /// release mode. - final String debugName; + final String? debugName; /// Finds a known [PhysicalKeyboardKey] that matches the given USB HID usage /// code. - static PhysicalKeyboardKey findKeyByCode(int usageCode) => _knownPhysicalKeys[usageCode]; + static PhysicalKeyboardKey? findKeyByCode(int usageCode) => _knownPhysicalKeys[usageCode]; @override int get hashCode => usbHidUsage.hashCode; diff --git a/packages/flutter/lib/src/services/keyboard_maps.dart b/packages/flutter/lib/src/services/keyboard_maps.dart index a1da1ba392..01266317bb 100644 --- a/packages/flutter/lib/src/services/keyboard_maps.dart +++ b/packages/flutter/lib/src/services/keyboard_maps.dart @@ -2,7 +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 // DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT // This file is generated by dev/tools/gen_keycodes/bin/gen_keycodes.dart and diff --git a/packages/flutter/lib/src/services/message_codec.dart b/packages/flutter/lib/src/services/message_codec.dart index 0d4a33597a..fb61573d75 100644 --- a/packages/flutter/lib/src/services/message_codec.dart +++ b/packages/flutter/lib/src/services/message_codec.dart @@ -2,7 +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:typed_data'; @@ -25,12 +24,12 @@ abstract class MessageCodec { /// Encodes the specified [message] in binary. /// /// Returns null if the message is null. - ByteData encodeMessage(T message); + ByteData? encodeMessage(T message); /// Decodes the specified [message] from binary. /// /// Returns null if the message is null. - T decodeMessage(ByteData message); + T decodeMessage(ByteData? message); } /// An command object representing the invocation of a named method. @@ -68,7 +67,7 @@ abstract class MethodCodec { ByteData encodeMethodCall(MethodCall methodCall); /// Decodes the specified [methodCall] from binary. - MethodCall decodeMethodCall(ByteData methodCall); + MethodCall decodeMethodCall(ByteData? methodCall); /// Decodes the specified result [envelope] from binary. /// @@ -83,7 +82,7 @@ abstract class MethodCodec { /// /// The specified error [code], human-readable error [message], and error /// [details] correspond to the fields of [PlatformException]. - ByteData encodeErrorEnvelope({ @required String code, String message, dynamic details }); + ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details }); } @@ -105,7 +104,7 @@ class PlatformException implements Exception { /// [message], and with the optional error [details] which must be a valid /// value for the [MethodCodec] involved in the interaction. PlatformException({ - @required this.code, + required this.code, this.message, this.details, }) : assert(code != null); @@ -114,7 +113,7 @@ class PlatformException implements Exception { final String code; /// A human-readable error message, possibly null. - final String message; + final String? message; /// Error details, possibly null. final dynamic details; @@ -139,7 +138,7 @@ class MissingPluginException implements Exception { MissingPluginException([this.message]); /// A human-readable error message, possibly null. - final String message; + final String? message; @override String toString() => 'MissingPluginException($message)'; diff --git a/packages/flutter/lib/src/services/message_codecs.dart b/packages/flutter/lib/src/services/message_codecs.dart index f11c34f971..cff3924f38 100644 --- a/packages/flutter/lib/src/services/message_codecs.dart +++ b/packages/flutter/lib/src/services/message_codecs.dart @@ -2,12 +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:convert'; import 'dart:typed_data'; -import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer, required; +import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer; import 'message_codec.dart'; @@ -19,35 +18,35 @@ import 'message_codec.dart'; /// When sending outgoing messages from Android, be sure to use direct `ByteBuffer` /// as opposed to indirect. The `wrap()` API provides indirect buffers by default /// and you will get empty `ByteData` objects in Dart. -class BinaryCodec implements MessageCodec { +class BinaryCodec implements MessageCodec { /// Creates a [MessageCodec] with unencoded binary messages represented using /// [ByteData]. const BinaryCodec(); @override - ByteData decodeMessage(ByteData message) => message; + ByteData? decodeMessage(ByteData? message) => message; @override - ByteData encodeMessage(ByteData message) => message; + ByteData? encodeMessage(ByteData? message) => message; } /// [MessageCodec] with UTF-8 encoded String messages. /// /// On Android, messages will be represented using `java.util.String`. /// On iOS, messages will be represented using `NSString`. -class StringCodec implements MessageCodec { +class StringCodec implements MessageCodec { /// Creates a [MessageCodec] with UTF-8 encoded String messages. const StringCodec(); @override - String decodeMessage(ByteData message) { + String? decodeMessage(ByteData? message) { if (message == null) return null; return utf8.decoder.convert(message.buffer.asUint8List(message.offsetInBytes, message.lengthInBytes)); } @override - ByteData encodeMessage(String message) { + ByteData? encodeMessage(String? message) { if (message == null) return null; final Uint8List encoded = utf8.encoder.convert(message); @@ -82,17 +81,17 @@ class JSONMessageCodec implements MessageCodec { const JSONMessageCodec(); @override - ByteData encodeMessage(dynamic message) { + ByteData? encodeMessage(dynamic message) { if (message == null) return null; return const StringCodec().encodeMessage(json.encode(message)); } @override - dynamic decodeMessage(ByteData message) { + dynamic decodeMessage(ByteData? message) { if (message == null) return message; - return json.decode(const StringCodec().decodeMessage(message)); + return json.decode(const StringCodec().decodeMessage(message)!); } } @@ -123,11 +122,11 @@ class JSONMethodCodec implements MethodCodec { return const JSONMessageCodec().encodeMessage({ 'method': call.method, 'args': call.arguments, - }); + })!; } @override - MethodCall decodeMethodCall(ByteData methodCall) { + MethodCall decodeMethodCall(ByteData? methodCall) { final dynamic decoded = const JSONMessageCodec().decodeMessage(methodCall); if (decoded is! Map) throw FormatException('Expected method call Map, got $decoded'); @@ -158,13 +157,13 @@ class JSONMethodCodec implements MethodCodec { @override ByteData encodeSuccessEnvelope(dynamic result) { - return const JSONMessageCodec().encodeMessage([result]); + return const JSONMessageCodec().encodeMessage([result])!; } @override - ByteData encodeErrorEnvelope({ @required String code, String message, dynamic details }) { + ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details }) { assert(code != null); - return const JSONMessageCodec().encodeMessage([code, message, details]); + return const JSONMessageCodec().encodeMessage([code, message, details])!; } } @@ -281,7 +280,7 @@ class StandardMessageCodec implements MessageCodec { static const int _valueMap = 13; @override - ByteData encodeMessage(dynamic message) { + ByteData? encodeMessage(dynamic message) { if (message == null) return null; final WriteBuffer buffer = WriteBuffer(); @@ -290,7 +289,7 @@ class StandardMessageCodec implements MessageCodec { } @override - dynamic decodeMessage(ByteData message) { + dynamic decodeMessage(ByteData? message) { if (message == null) return null; final ReadBuffer buffer = ReadBuffer(message); @@ -444,7 +443,7 @@ class StandardMessageCodec implements MessageCodec { return buffer.getFloat64List(length); case _valueList: final int length = readSize(buffer); - final dynamic result = List(length); + final dynamic result = List.filled(length, null, growable: false); for (int i = 0; i < length; i++) result[i] = readValue(buffer); return result; @@ -529,8 +528,8 @@ class StandardMethodCodec implements MethodCodec { } @override - MethodCall decodeMethodCall(ByteData methodCall) { - final ReadBuffer buffer = ReadBuffer(methodCall); + MethodCall decodeMethodCall(ByteData? methodCall) { + final ReadBuffer buffer = ReadBuffer(methodCall!); final dynamic method = messageCodec.readValue(buffer); final dynamic arguments = messageCodec.readValue(buffer); if (method is String && !buffer.hasRemaining) @@ -548,7 +547,7 @@ class StandardMethodCodec implements MethodCodec { } @override - ByteData encodeErrorEnvelope({ @required String code, String message, dynamic details }) { + ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details }) { final WriteBuffer buffer = WriteBuffer(); buffer.putUint8(1); messageCodec.writeValue(buffer, code); diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart index 74cfe701b8..bf2581faf2 100644 --- a/packages/flutter/lib/src/services/platform_channel.dart +++ b/packages/flutter/lib/src/services/platform_channel.dart @@ -2,7 +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:typed_data'; @@ -36,7 +35,7 @@ class BasicMessageChannel { /// /// The [name] and [codec] arguments cannot be null. The default [ServicesBinding.defaultBinaryMessenger] /// instance is used if [binaryMessenger] is null. - const BasicMessageChannel(this.name, this.codec, { BinaryMessenger binaryMessenger }) + const BasicMessageChannel(this.name, this.codec, { BinaryMessenger? binaryMessenger }) : assert(name != null), assert(codec != null), _binaryMessenger = binaryMessenger; @@ -49,7 +48,7 @@ class BasicMessageChannel { /// The messenger which sends the bytes for this channel, not null. BinaryMessenger get binaryMessenger => _binaryMessenger ?? defaultBinaryMessenger; - final BinaryMessenger _binaryMessenger; + final BinaryMessenger? _binaryMessenger; /// Sends the specified [message] to the platform plugins on this channel. /// @@ -72,7 +71,7 @@ class BasicMessageChannel { if (handler == null) { binaryMessenger.setMessageHandler(name, null); } else { - binaryMessenger.setMessageHandler(name, (ByteData message) async { + binaryMessenger.setMessageHandler(name, (ByteData? message) async { return codec.encodeMessage(await handler(codec.decodeMessage(message))); }); } @@ -93,7 +92,7 @@ class BasicMessageChannel { if (handler == null) { binaryMessenger.setMockMessageHandler(name, null); } else { - binaryMessenger.setMockMessageHandler(name, (ByteData message) async { + binaryMessenger.setMockMessageHandler(name, (ByteData? message) async { return codec.encodeMessage(await handler(codec.decodeMessage(message))); }); } @@ -128,7 +127,7 @@ class MethodChannel { /// /// The [name] and [codec] arguments cannot be null. The default [ServicesBinding.defaultBinaryMessenger] /// instance is used if [binaryMessenger] is null. - const MethodChannel(this.name, [this.codec = const StandardMethodCodec(), BinaryMessenger binaryMessenger ]) + const MethodChannel(this.name, [this.codec = const StandardMethodCodec(), BinaryMessenger? binaryMessenger ]) : assert(name != null), assert(codec != null), _binaryMessenger = binaryMessenger; @@ -143,12 +142,12 @@ class MethodChannel { /// /// The messenger may not be null. BinaryMessenger get binaryMessenger => _binaryMessenger ?? defaultBinaryMessenger; - final BinaryMessenger _binaryMessenger; + final BinaryMessenger? _binaryMessenger; @optionalTypeArgs - Future _invokeMethod(String method, { bool missingOk, dynamic arguments }) async { + Future _invokeMethod(String method, { required bool missingOk, dynamic arguments }) async { assert(method != null); - final ByteData result = await binaryMessenger.send( + final ByteData? result = await binaryMessenger.send( name, codec.encodeMethodCall(MethodCall(method, arguments)), ); @@ -330,7 +329,7 @@ class MethodChannel { /// * /// for how to access method call arguments on Android. @optionalTypeArgs - Future invokeMethod(String method, [ dynamic arguments ]) { + Future invokeMethod(String method, [ dynamic arguments ]) { return _invokeMethod(method, missingOk: false, arguments: arguments); } @@ -343,8 +342,8 @@ class MethodChannel { /// See also: /// /// * [invokeMethod], which this call delegates to. - Future> invokeListMethod(String method, [ dynamic arguments ]) async { - final List result = await invokeMethod>(method, arguments); + Future?> invokeListMethod(String method, [ dynamic arguments ]) async { + final List? result = await invokeMethod?>(method, arguments); return result?.cast(); } @@ -357,8 +356,8 @@ class MethodChannel { /// See also: /// /// * [invokeMethod], which this call delegates to. - Future> invokeMapMethod(String method, [ dynamic arguments ]) async { - final Map result = await invokeMethod>(method, arguments); + Future?> invokeMapMethod(String method, [ dynamic arguments ]) async { + final Map? result = await invokeMethod?>(method, arguments); return result?.cast(); } @@ -376,13 +375,13 @@ class MethodChannel { /// completes with a [MissingPluginException], an empty reply is sent /// similarly to what happens if no method call handler has been set. /// Any other exception results in an error envelope being sent. - void setMethodCallHandler(Future handler(MethodCall call)) { + void setMethodCallHandler(Future Function(MethodCall call)? handler) { _methodChannelHandlers[this] = handler; binaryMessenger.setMessageHandler( name, handler == null ? null - : (ByteData message) => _handleAsMethodCall(message, handler), + : (ByteData? message) => _handleAsMethodCall(message, handler), ); } @@ -415,7 +414,7 @@ class MethodChannel { _methodChannelMockHandlers[this] = handler; binaryMessenger.setMockMessageHandler( name, - handler == null ? null : (ByteData message) => _handleAsMethodCall(message, handler), + handler == null ? null : (ByteData? message) => _handleAsMethodCall(message, handler), ); } @@ -426,7 +425,7 @@ class MethodChannel { /// handler for the specified channel has not been altered by a previous test. bool checkMockMethodCallHandler(Future handler(MethodCall call)) => _methodChannelMockHandlers[this] == handler; - Future _handleAsMethodCall(ByteData message, Future handler(MethodCall call)) async { + Future _handleAsMethodCall(ByteData? message, Future handler(MethodCall call)) async { final MethodCall call = codec.decodeMethodCall(message); try { return codec.encodeSuccessEnvelope(await handler(call)); @@ -454,20 +453,20 @@ class OptionalMethodChannel extends MethodChannel { : super(name, codec); @override - Future invokeMethod(String method, [ dynamic arguments ]) async { + Future invokeMethod(String method, [ dynamic arguments ]) async { return super._invokeMethod(method, missingOk: true, arguments: arguments); } @override - Future> invokeListMethod(String method, [ dynamic arguments ]) async { - final List result = await invokeMethod>(method, arguments); - return result.cast(); + Future?> invokeListMethod(String method, [ dynamic arguments ]) async { + final List? result = await invokeMethod>(method, arguments); + return result?.cast(); } @override - Future> invokeMapMethod(String method, [ dynamic arguments ]) async { - final Map result = await invokeMethod>(method, arguments); - return result.cast(); + Future?> invokeMapMethod(String method, [ dynamic arguments ]) async { + final Map? result = await invokeMethod>(method, arguments); + return result?.cast(); } } @@ -494,7 +493,7 @@ class EventChannel { /// /// Neither [name] nor [codec] may be null. The default [ServicesBinding.defaultBinaryMessenger] /// instance is used if [binaryMessenger] is null. - const EventChannel(this.name, [this.codec = const StandardMethodCodec(), BinaryMessenger binaryMessenger]) + const EventChannel(this.name, [this.codec = const StandardMethodCodec(), BinaryMessenger? binaryMessenger]) : assert(name != null), assert(codec != null), _binaryMessenger = binaryMessenger; @@ -507,7 +506,7 @@ class EventChannel { /// The messenger used by this channel to send platform messages, not null. BinaryMessenger get binaryMessenger => _binaryMessenger ?? defaultBinaryMessenger; - final BinaryMessenger _binaryMessenger; + final BinaryMessenger? _binaryMessenger; /// Sets up a broadcast stream for receiving events on this channel. /// @@ -524,9 +523,9 @@ class EventChannel { /// only when stream listener count changes from 1 to 0. Stream receiveBroadcastStream([ dynamic arguments ]) { final MethodChannel methodChannel = MethodChannel(name, codec); - StreamController controller; + late StreamController controller; controller = StreamController.broadcast(onListen: () async { - binaryMessenger.setMessageHandler(name, (ByteData reply) async { + binaryMessenger.setMessageHandler(name, (ByteData? reply) async { if (reply == null) { controller.close(); } else { diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart index c4cd43b482..1ad7ce856a 100644 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ b/packages/flutter/lib/src/services/platform_messages.dart @@ -2,7 +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:typed_data'; @@ -67,7 +66,7 @@ class BinaryMessages { 'Use defaultBinaryMessenger.send instead. ' 'This feature was deprecated after v1.6.5.' ) - static Future send(String channel, ByteData message) { + static Future send(String channel, ByteData? message) { return _binaryMessenger.send(channel, message); } @@ -83,7 +82,7 @@ class BinaryMessages { 'Use defaultBinaryMessenger.setMessageHandler instead. ' 'This feature was deprecated after v1.6.5.' ) - static void setMessageHandler(String channel, Future handler(ByteData message)) { + static void setMessageHandler(String channel, Future Function(ByteData? message) handler) { _binaryMessenger.setMessageHandler(channel, handler); } @@ -102,7 +101,7 @@ class BinaryMessages { 'Use defaultBinaryMessenger.setMockMessageHandler instead. ' 'This feature was deprecated after v1.6.5.' ) - static void setMockMessageHandler(String channel, Future handler(ByteData message)) { + static void setMockMessageHandler(String channel, Future Function(ByteData? message) handler) { _binaryMessenger.setMockMessageHandler(channel, handler); } } diff --git a/packages/flutter/lib/src/services/platform_views.dart b/packages/flutter/lib/src/services/platform_views.dart index 1ad1bbcdfb..5badb4fdb9 100644 --- a/packages/flutter/lib/src/services/platform_views.dart +++ b/packages/flutter/lib/src/services/platform_views.dart @@ -2,7 +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:typed_data'; @@ -57,25 +56,20 @@ class PlatformViewsService { SystemChannels.platform_views.setMethodCallHandler(_onMethodCall); } - static PlatformViewsService _serviceInstance; - - static PlatformViewsService get _instance { - _serviceInstance ??= PlatformViewsService._(); - return _serviceInstance; - } + static final PlatformViewsService _instance = PlatformViewsService._(); Future _onMethodCall(MethodCall call) { switch(call.method) { case 'viewFocused': final int id = call.arguments as int; if (_focusCallbacks.containsKey(id)) { - _focusCallbacks[id](); + _focusCallbacks[id]!(); } break; default: throw UnimplementedError("${call.method} was invoked but isn't implemented by PlatformViewsService"); } - return null; + return Future.value(); } /// Maps platform view IDs to focus callbacks. @@ -111,12 +105,12 @@ class PlatformViewsService { /// The `id, `viewType, and `layoutDirection` parameters must not be null. /// If `creationParams` is non null then `creationParamsCodec` must not be null. static TextureAndroidViewController initAndroidView({ - @required int id, - @required String viewType, - @required TextDirection layoutDirection, + required int id, + required String viewType, + required TextDirection layoutDirection, dynamic creationParams, - MessageCodec creationParamsCodec, - VoidCallback onFocus, + MessageCodec? creationParamsCodec, + VoidCallback? onFocus, }) { assert(id != null); assert(viewType != null); @@ -162,12 +156,12 @@ class PlatformViewsService { /// The `id, `viewType, and `layoutDirection` parameters must not be null. /// If `creationParams` is non null then `creationParamsCodec` must not be null. static SurfaceAndroidViewController initSurfaceAndroidView({ - @required int id, - @required String viewType, - @required TextDirection layoutDirection, + required int id, + required String viewType, + required TextDirection layoutDirection, dynamic creationParams, - MessageCodec creationParamsCodec, - VoidCallback onFocus, + MessageCodec? creationParamsCodec, + VoidCallback? onFocus, }) { assert(id != null); assert(viewType != null); @@ -198,11 +192,11 @@ class PlatformViewsService { /// The `id, `viewType, and `layoutDirection` parameters must not be null. /// If `creationParams` is non null then `creationParamsCodec` must not be null. static Future initUiKitView({ - @required int id, - @required String viewType, - @required TextDirection layoutDirection, + required int id, + required String viewType, + required TextDirection layoutDirection, dynamic creationParams, - MessageCodec creationParamsCodec, + MessageCodec? creationParamsCodec, }) async { assert(id != null); assert(viewType != null); @@ -215,7 +209,7 @@ class PlatformViewsService { 'viewType': viewType, }; if (creationParams != null) { - final ByteData paramsByteData = creationParamsCodec.encodeMessage(creationParams); + final ByteData paramsByteData = creationParamsCodec!.encodeMessage(creationParams)!; args['params'] = Uint8List.view( paramsByteData.buffer, 0, @@ -235,8 +229,8 @@ class AndroidPointerProperties { /// /// All parameters must not be null. const AndroidPointerProperties({ - @required this.id, - @required this.toolType, + required this.id, + required this.toolType, }) : assert(id != null), assert(toolType != null); @@ -278,15 +272,15 @@ class AndroidPointerCoords { /// /// All parameters must not be null. const AndroidPointerCoords({ - @required this.orientation, - @required this.pressure, - @required this.size, - @required this.toolMajor, - @required this.toolMinor, - @required this.touchMajor, - @required this.touchMinor, - @required this.x, - @required this.y, + required this.orientation, + required this.pressure, + required this.size, + required this.toolMajor, + required this.toolMinor, + required this.touchMajor, + required this.touchMinor, + required this.x, + required this.y, }) : assert(orientation != null), assert(pressure != null), assert(size != null), @@ -360,21 +354,21 @@ class AndroidMotionEvent { /// /// All parameters must not be null. AndroidMotionEvent({ - @required this.downTime, - @required this.eventTime, - @required this.action, - @required this.pointerCount, - @required this.pointerProperties, - @required this.pointerCoords, - @required this.metaState, - @required this.buttonState, - @required this.xPrecision, - @required this.yPrecision, - @required this.deviceId, - @required this.edgeFlags, - @required this.source, - @required this.flags, - @required this.motionEventId, + required this.downTime, + required this.eventTime, + required this.action, + required this.pointerCount, + required this.pointerProperties, + required this.pointerCoords, + required this.metaState, + required this.buttonState, + required this.xPrecision, + required this.yPrecision, + required this.deviceId, + required this.edgeFlags, + required this.source, + required this.flags, + required this.motionEventId, }) : assert(downTime != null), assert(eventTime != null), assert(action != null), @@ -503,14 +497,14 @@ class _AndroidMotionEventConverter { {}; final Set usedAndroidPointerIds = {}; - PointTransformer _pointTransformer; + late PointTransformer _pointTransformer; set pointTransformer(PointTransformer transformer) { assert(transformer != null); _pointTransformer = transformer; } - int downTimeMillis; + int? downTimeMillis; void handlePointerDownEvent(PointerDownEvent event) { if (pointerProperties.isEmpty) { @@ -525,7 +519,6 @@ class _AndroidMotionEventConverter { } void updatePointerPositions(PointerEvent event) { - assert(_pointTransformer != null); final Offset position = _pointTransformer(event.position); pointerPositions[event.pointer] = AndroidPointerCoords( orientation: event.orientation, @@ -542,7 +535,7 @@ class _AndroidMotionEventConverter { void handlePointerUpEvent(PointerUpEvent event) { pointerPositions.remove(event.pointer); - usedAndroidPointerIds.remove(pointerProperties[event.pointer].id); + usedAndroidPointerIds.remove(pointerProperties[event.pointer]!.id); pointerProperties.remove(event.pointer); if (pointerProperties.isEmpty) { downTimeMillis = null; @@ -556,7 +549,7 @@ class _AndroidMotionEventConverter { downTimeMillis = null; } - AndroidMotionEvent toAndroidMotionEvent(PointerEvent event) { + AndroidMotionEvent? toAndroidMotionEvent(PointerEvent event) { final List pointers = pointerPositions.keys.toList(); final int pointerIdx = pointers.indexOf(event.pointer); final int numPointers = pointers.length; @@ -600,15 +593,15 @@ class _AndroidMotionEventConverter { } return AndroidMotionEvent( - downTime: downTimeMillis, + downTime: downTimeMillis!, eventTime: event.timeStamp.inMilliseconds, action: action, pointerCount: pointerPositions.length, pointerProperties: pointers - .map((int i) => pointerProperties[i]) + .map((int i) => pointerProperties[i]!) .toList(), pointerCoords: pointers - .map((int i) => pointerPositions[i]) + .map((int i) => pointerPositions[i]!) .toList(), metaState: 0, buttonState: 0, @@ -654,11 +647,11 @@ class _AndroidMotionEventConverter { // TODO(bparrishMines): Remove abstract methods that are not required by all subclasses. abstract class AndroidViewController extends PlatformViewController { AndroidViewController._({ - @required this.viewId, - @required String viewType, - @required TextDirection layoutDirection, + required this.viewId, + required String viewType, + required TextDirection layoutDirection, dynamic creationParams, - MessageCodec creationParamsCodec, + MessageCodec? creationParamsCodec, bool waitingForSize = false, }) : assert(viewId != null), assert(viewType != null), @@ -724,7 +717,7 @@ abstract class AndroidViewController extends PlatformViewController { final dynamic _creationParams; - final MessageCodec _creationParamsCodec; + final MessageCodec? _creationParamsCodec; final List _platformViewCreatedCallbacks = []; @@ -737,7 +730,6 @@ abstract class AndroidViewController extends PlatformViewController { case TextDirection.rtl: return kAndroidLayoutDirectionRtl; } - return null; } /// Creates a masked Android MotionEvent action value for an indexed pointer. @@ -774,7 +766,7 @@ abstract class AndroidViewController extends PlatformViewController { /// /// Returns null if the Android view has not been successfully created, or if it has been /// disposed. - int get textureId; + int? get textureId; /// The unique identifier of the Android view controlled by this controller. @Deprecated( @@ -862,15 +854,13 @@ abstract class AndroidViewController extends PlatformViewController { /// for description of the parameters. @override Future dispatchPointerEvent(PointerEvent event) async { - assert(_motionEventConverter._pointTransformer != null); - if (event is PointerDownEvent) { _motionEventConverter.handlePointerDownEvent(event); } _motionEventConverter.updatePointerPositions(event); - final AndroidMotionEvent androidEvent = + final AndroidMotionEvent? androidEvent = _motionEventConverter.toAndroidMotionEvent(event); if (event is PointerUpEvent) { @@ -888,7 +878,7 @@ abstract class AndroidViewController extends PlatformViewController { @override Future clearFocus() { if (_state != _AndroidViewState.created) { - return null; + return Future.value(); } return SystemChannels.platform_views.invokeMethod('clearFocus', viewId); } @@ -913,11 +903,11 @@ abstract class AndroidViewController extends PlatformViewController { /// Typically created with [PlatformViewsService.initAndroidView]. class SurfaceAndroidViewController extends AndroidViewController { SurfaceAndroidViewController._({ - @required int viewId, - @required String viewType, - @required TextDirection layoutDirection, + required int viewId, + required String viewType, + required TextDirection layoutDirection, dynamic creationParams, - MessageCodec creationParamsCodec, + MessageCodec? creationParamsCodec, }) : super._( viewId: viewId, viewType: viewType, @@ -935,7 +925,7 @@ class SurfaceAndroidViewController extends AndroidViewController { }; if (_creationParams != null) { final ByteData paramsByteData = - _creationParamsCodec.encodeMessage(_creationParams); + _creationParamsCodec!.encodeMessage(_creationParams)!; args['params'] = Uint8List.view( paramsByteData.buffer, 0, @@ -973,11 +963,11 @@ class SurfaceAndroidViewController extends AndroidViewController { /// Typically created with [PlatformViewsService.initAndroidView]. class TextureAndroidViewController extends AndroidViewController { TextureAndroidViewController._({ - @required int viewId, - @required String viewType, - @required TextDirection layoutDirection, + required int viewId, + required String viewType, + required TextDirection layoutDirection, dynamic creationParams, - MessageCodec creationParamsCodec, + MessageCodec? creationParamsCodec, }) : super._( viewId: viewId, viewType: viewType, @@ -988,16 +978,16 @@ class TextureAndroidViewController extends AndroidViewController { ); /// The texture entry id into which the Android view is rendered. - int _textureId; + int? _textureId; /// Returns the texture entry id that the Android view is rendering into. /// /// Returns null if the Android view has not been successfully created, or if it has been /// disposed. @override - int get textureId => _textureId; + int? get textureId => _textureId; - Size _size; + late Size _size; @override Future setSize(Size size) async { @@ -1029,8 +1019,7 @@ class TextureAndroidViewController extends AndroidViewController { @override Future _sendCreateMessage() async { - assert(_size != null && !_size.isEmpty, - 'trying to create $TextureAndroidViewController without setting a valid size.'); + assert(!_size.isEmpty, 'trying to create $TextureAndroidViewController without setting a valid size.'); final Map args = { 'id': viewId, @@ -1040,7 +1029,7 @@ class TextureAndroidViewController extends AndroidViewController { 'direction': AndroidViewController._getAndroidDirection(_layoutDirection), }; if (_creationParams != null) { - final ByteData paramsByteData = _creationParamsCodec.encodeMessage(_creationParams); + final ByteData paramsByteData = _creationParamsCodec!.encodeMessage(_creationParams)!; args['params'] = Uint8List.view( paramsByteData.buffer, 0, diff --git a/packages/flutter/lib/src/services/raw_keyboard.dart b/packages/flutter/lib/src/services/raw_keyboard.dart index 834ec50deb..430a548426 100644 --- a/packages/flutter/lib/src/services/raw_keyboard.dart +++ b/packages/flutter/lib/src/services/raw_keyboard.dart @@ -2,7 +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:io'; @@ -138,7 +137,7 @@ abstract class RawKeyEventData { /// null. If the given key only appears in one place on the keyboard, returns /// [KeyboardSide.all] if pressed. Never returns [KeyboardSide.any], because /// that doesn't make sense in this context. - KeyboardSide getModifierSide(ModifierKey key); + KeyboardSide? getModifierSide(ModifierKey key); /// Returns true if a CTRL modifier key was pressed at the time of this event, /// regardless of which side of the keyboard it is on. @@ -166,8 +165,8 @@ abstract class RawKeyEventData { /// Returns a map of modifier keys that were pressed at the time of this /// event, and the keyboard side or sides that the key was on. - Map get modifiersPressed { - final Map result = {}; + Map get modifiersPressed { + final Map result = {}; for (final ModifierKey key in ModifierKey.values) { if (isModifierPressed(key)) { result[key] = getModifierSide(key); @@ -212,7 +211,7 @@ abstract class RawKeyEventData { /// complexities of managing keyboard input, like showing a soft keyboard or /// interacting with an input method editor (IME). /// {@endtemplate} - String get keyLabel; + String? get keyLabel; } /// Defines the interface for raw key events. @@ -243,7 +242,7 @@ abstract class RawKeyEvent with Diagnosticable { /// Initializes fields for subclasses, and provides a const constructor for /// const subclasses. const RawKeyEvent({ - @required this.data, + required this.data, this.character, }); @@ -256,40 +255,40 @@ abstract class RawKeyEvent with Diagnosticable { switch (keymap) { case 'android': data = RawKeyEventDataAndroid( - flags: message['flags'] as int ?? 0, - codePoint: message['codePoint'] as int ?? 0, - keyCode: message['keyCode'] as int ?? 0, - plainCodePoint: message['plainCodePoint'] as int ?? 0, - scanCode: message['scanCode'] as int ?? 0, - metaState: message['metaState'] as int ?? 0, - eventSource: message['source'] as int ?? 0, - vendorId: message['vendorId'] as int ?? 0, - productId: message['productId'] as int ?? 0, - deviceId: message['deviceId'] as int ?? 0, - repeatCount: message['repeatCount'] as int ?? 0, + flags: message['flags'] as int? ?? 0, + codePoint: message['codePoint'] as int? ?? 0, + keyCode: message['keyCode'] as int? ?? 0, + plainCodePoint: message['plainCodePoint'] as int? ?? 0, + scanCode: message['scanCode'] as int? ?? 0, + metaState: message['metaState'] as int? ?? 0, + eventSource: message['source'] as int? ?? 0, + vendorId: message['vendorId'] as int? ?? 0, + productId: message['productId'] as int? ?? 0, + deviceId: message['deviceId'] as int? ?? 0, + repeatCount: message['repeatCount'] as int? ?? 0, ); break; case 'fuchsia': data = RawKeyEventDataFuchsia( - hidUsage: message['hidUsage'] as int ?? 0, - codePoint: message['codePoint'] as int ?? 0, - modifiers: message['modifiers'] as int ?? 0, + hidUsage: message['hidUsage'] as int? ?? 0, + codePoint: message['codePoint'] as int? ?? 0, + modifiers: message['modifiers'] as int? ?? 0, ); break; case 'macos': data = RawKeyEventDataMacOs( - characters: message['characters'] as String ?? '', - charactersIgnoringModifiers: message['charactersIgnoringModifiers'] as String ?? '', - keyCode: message['keyCode'] as int ?? 0, - modifiers: message['modifiers'] as int ?? 0); + characters: message['characters'] as String? ?? '', + charactersIgnoringModifiers: message['charactersIgnoringModifiers'] as String? ?? '', + keyCode: message['keyCode'] as int? ?? 0, + modifiers: message['modifiers'] as int? ?? 0); break; case 'linux': data = RawKeyEventDataLinux( - keyHelper: KeyHelper(message['toolkit'] as String ?? ''), - unicodeScalarValues: message['unicodeScalarValues'] as int ?? 0, - keyCode: message['keyCode'] as int ?? 0, - scanCode: message['scanCode'] as int ?? 0, - modifiers: message['modifiers'] as int ?? 0, + keyHelper: KeyHelper(message['toolkit'] as String? ?? ''), + unicodeScalarValues: message['unicodeScalarValues'] as int? ?? 0, + keyCode: message['keyCode'] as int? ?? 0, + scanCode: message['scanCode'] as int? ?? 0, + modifiers: message['modifiers'] as int? ?? 0, isDown: message['type'] == 'keydown'); break; case 'web': @@ -428,7 +427,7 @@ abstract class RawKeyEvent with Diagnosticable { /// composing text, use the [TextField] or [CupertinoTextField] widgets, since /// those automatically handle many of the complexities of managing keyboard /// input. - final String character; + final String? character; /// Platform-specific information about the key event. final RawKeyEventData data; @@ -449,8 +448,8 @@ abstract class RawKeyEvent with Diagnosticable { class RawKeyDownEvent extends RawKeyEvent { /// Creates a key event that represents the user pressing a key. const RawKeyDownEvent({ - @required RawKeyEventData data, - String character, + required RawKeyEventData data, + String? character, }) : super(data: data, character: character); } @@ -462,8 +461,8 @@ class RawKeyDownEvent extends RawKeyEvent { class RawKeyUpEvent extends RawKeyEvent { /// Creates a key event that represents the user releasing a key. const RawKeyUpEvent({ - @required RawKeyEventData data, - String character, + required RawKeyEventData data, + String? character, }) : super(data: data, character: character); } @@ -557,13 +556,10 @@ class RawKeyboard { /// focus. /// * [addListener], to add passive key event listeners that do not stop event /// propagation. - RawKeyEventHandler keyEventHandler; + RawKeyEventHandler? keyEventHandler; Future _handleKeyEvent(dynamic message) async { final RawKeyEvent event = RawKeyEvent.fromMessage(message as Map); - if (event == null) { - return; - } if (event.data is RawKeyEventDataMacOs && event.logicalKey == LogicalKeyboardKey.fn) { // On macOS laptop keyboards, the fn key is used to generate home/end and // f1-f12, but it ALSO generates a separate down/up event for the fn key @@ -595,7 +591,7 @@ class RawKeyboard { // Send the key event to the keyEventHandler, then send the appropriate // response to the platform so that it can resolve the event's handling. // Defaults to false if keyEventHandler is null. - final bool handled = keyEventHandler != null && keyEventHandler(event); + final bool handled = keyEventHandler != null && keyEventHandler!(event); assert(handled != null, 'keyEventHandler returned null, which is not allowed'); return { 'handled': handled }; } @@ -656,15 +652,15 @@ class RawKeyboard { // pressed/released while the app doesn't have focus, to make sure that // _keysPressed reflects reality at all times. - final Map modifiersPressed = event.data.modifiersPressed; + final Map modifiersPressed = event.data.modifiersPressed; final Map modifierKeys = {}; for (final ModifierKey key in modifiersPressed.keys) { - final Set mappedKeys = _modifierKeyMap[_ModifierSidePair(key, modifiersPressed[key])]; + final Set mappedKeys = _modifierKeyMap[_ModifierSidePair(key, modifiersPressed[key])]!; assert(mappedKeys != null, 'Platform key support for ${Platform.operatingSystem} is ' 'producing unsupported modifier combinations.'); for (final PhysicalKeyboardKey physicalModifier in mappedKeys) { - modifierKeys[physicalModifier] = _allModifiers[physicalModifier]; + modifierKeys[physicalModifier] = _allModifiers[physicalModifier]!; } } _allModifiersExceptFn.keys.forEach(_keysPressed.remove); @@ -695,7 +691,7 @@ class _ModifierSidePair extends Object { const _ModifierSidePair(this.modifier, this.side); final ModifierKey modifier; - final KeyboardSide side; + final KeyboardSide? side; @override bool operator ==(Object other) { diff --git a/packages/flutter/lib/src/services/raw_keyboard_android.dart b/packages/flutter/lib/src/services/raw_keyboard_android.dart index 14802b3478..b1e6b50ada 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_android.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_android.dart @@ -2,7 +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:flutter/foundation.dart'; @@ -148,7 +147,7 @@ class RawKeyEventDataAndroid extends RawKeyEventData { // Android only reports a single code point for the key label. @override - String get keyLabel => plainCodePoint == 0 ? null : String.fromCharCode(plainCodePoint & _kCombiningCharacterMask); + String? get keyLabel => plainCodePoint == 0 ? null : String.fromCharCode(plainCodePoint & _kCombiningCharacterMask); @override PhysicalKeyboardKey get physicalKey { @@ -161,7 +160,7 @@ class RawKeyEventDataAndroid extends RawKeyEventData { // our own DPAD physical keys. The logical key will still match "arrowUp", // etc. if (eventSource & _sourceJoystick == _sourceJoystick) { - final LogicalKeyboardKey foundKey = kAndroidToLogicalKey[keyCode]; + final LogicalKeyboardKey? foundKey = kAndroidToLogicalKey[keyCode]; if (foundKey == LogicalKeyboardKey.arrowUp) { return PhysicalKeyboardKey.arrowUp; } @@ -183,7 +182,7 @@ class RawKeyEventDataAndroid extends RawKeyEventData { // Look to see if the keyCode is a printable number pad key, so that a // difference between regular keys (e.g. "=") and the number pad version // (e.g. the "=" on the number pad) can be determined. - final LogicalKeyboardKey numPadKey = kAndroidNumPadMap[keyCode]; + final LogicalKeyboardKey? numPadKey = kAndroidNumPadMap[keyCode]; if (numPadKey != null) { return numPadKey; } @@ -192,18 +191,18 @@ class RawKeyEventDataAndroid extends RawKeyEventData { // constant, or construct a new Unicode-based key from it. Don't mark it as // autogenerated, since the label uniquely identifies an ID from the Unicode // plane. - if (keyLabel != null && keyLabel.isNotEmpty && !LogicalKeyboardKey.isControlCharacter(keyLabel)) { + if (keyLabel != null && keyLabel!.isNotEmpty && !LogicalKeyboardKey.isControlCharacter(keyLabel!)) { final int combinedCodePoint = plainCodePoint & _kCombiningCharacterMask; final int keyId = LogicalKeyboardKey.unicodePlane | (combinedCodePoint & LogicalKeyboardKey.valueMask); return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey( keyId, keyLabel: keyLabel, - debugName: kReleaseMode ? null : 'Key ${keyLabel.toUpperCase()}', + debugName: kReleaseMode ? null : 'Key ${keyLabel!.toUpperCase()}', ); } // Look to see if the keyCode is one we know about and have a mapping for. - LogicalKeyboardKey newKey = kAndroidToLogicalKey[keyCode]; + LogicalKeyboardKey? newKey = kAndroidToLogicalKey[keyCode]; if (newKey != null) { return newKey; } @@ -232,7 +231,6 @@ class RawKeyEventDataAndroid extends RawKeyEventData { case KeyboardSide.right: return metaState & rightMask != 0; } - return false; } @override @@ -258,12 +256,11 @@ class RawKeyEventDataAndroid extends RawKeyEventData { case ModifierKey.symbolModifier: return metaState & modifierSym != 0; } - return false; } @override - KeyboardSide getModifierSide(ModifierKey key) { - KeyboardSide findSide(int leftMask, int rightMask) { + KeyboardSide? getModifierSide(ModifierKey key) { + KeyboardSide? findSide(int leftMask, int rightMask) { final int combinedMask = leftMask | rightMask; final int combined = metaState & combinedMask; if (combined == leftMask) { @@ -292,9 +289,6 @@ class RawKeyEventDataAndroid extends RawKeyEventData { case ModifierKey.symbolModifier: return KeyboardSide.all; } - - assert(false, 'Not handling $key type properly.'); - return null; } // Modifier key masks. diff --git a/packages/flutter/lib/src/services/raw_keyboard_fuchsia.dart b/packages/flutter/lib/src/services/raw_keyboard_fuchsia.dart index ebeb68439f..16be9cda2a 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_fuchsia.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_fuchsia.dart @@ -2,7 +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:flutter/foundation.dart'; @@ -62,7 +61,7 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { // Fuchsia only reports a single code point for the key label. @override - String get keyLabel => codePoint == 0 ? null : String.fromCharCode(codePoint); + String? get keyLabel => codePoint == 0 ? null : String.fromCharCode(codePoint); @override LogicalKeyboardKey get logicalKey { @@ -77,7 +76,7 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { } // Look to see if the hidUsage is one we know about and have a mapping for. - LogicalKeyboardKey newKey = kFuchsiaToLogicalKey[hidUsage | LogicalKeyboardKey.hidPlane]; + LogicalKeyboardKey? newKey = kFuchsiaToLogicalKey[hidUsage | LogicalKeyboardKey.hidPlane]; if (newKey != null) { return newKey; } @@ -109,7 +108,6 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { case KeyboardSide.right: return modifiers & rightMask != 0; } - return false; } @override @@ -133,12 +131,11 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { // Fuchsia doesn't have masks for these keys (yet). return false; } - return false; } @override - KeyboardSide getModifierSide(ModifierKey key) { - KeyboardSide findSide(int leftMask, int rightMask, int combinedMask) { + KeyboardSide? getModifierSide(ModifierKey key) { + KeyboardSide? findSide(int leftMask, int rightMask, int combinedMask) { final int combined = modifiers & combinedMask; if (combined == leftMask) { return KeyboardSide.left; @@ -168,9 +165,6 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { // Fuchsia doesn't support these modifiers, so they can't be pressed. return null; } - - assert(false, 'Not handling $key type properly.'); - return null; } // Keyboard modifier masks for Fuchsia modifiers. diff --git a/packages/flutter/lib/src/services/raw_keyboard_linux.dart b/packages/flutter/lib/src/services/raw_keyboard_linux.dart index 67ff33a527..669f6c42bb 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_linux.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_linux.dart @@ -2,7 +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:flutter/foundation.dart'; @@ -24,12 +23,12 @@ class RawKeyEventDataLinux extends RawKeyEventData { /// The [keyHelper], [scanCode], [unicodeScalarValues], [keyCode], and [modifiers], /// arguments must not be null. const RawKeyEventDataLinux({ - @required this.keyHelper, + required this.keyHelper, this.unicodeScalarValues = 0, this.scanCode = 0, this.keyCode = 0, this.modifiers = 0, - @required this.isDown, + required this.isDown, }) : assert(scanCode != null), assert(unicodeScalarValues != null), assert((unicodeScalarValues & ~LogicalKeyboardKey.valueMask) == 0), @@ -71,7 +70,7 @@ class RawKeyEventDataLinux extends RawKeyEventData { final bool isDown; @override - String get keyLabel => unicodeScalarValues == 0 ? null : String.fromCharCode(unicodeScalarValues); + String? get keyLabel => unicodeScalarValues == 0 ? null : String.fromCharCode(unicodeScalarValues); @override PhysicalKeyboardKey get physicalKey => kLinuxToPhysicalKey[scanCode] ?? PhysicalKeyboardKey.none; @@ -81,7 +80,7 @@ class RawKeyEventDataLinux extends RawKeyEventData { // Look to see if the keyCode is a printable number pad key, so that a // difference between regular keys (e.g. "=") and the number pad version // (e.g. the "=" on the number pad) can be determined. - final LogicalKeyboardKey numPadKey = keyHelper.numpadKey(keyCode); + final LogicalKeyboardKey? numPadKey = keyHelper.numpadKey(keyCode); if (numPadKey != null) { return numPadKey; } @@ -91,17 +90,17 @@ class RawKeyEventDataLinux extends RawKeyEventData { // autogenerated, since the label uniquely identifies an ID from the Unicode // plane. if (keyLabel != null && - !LogicalKeyboardKey.isControlCharacter(keyLabel)) { + !LogicalKeyboardKey.isControlCharacter(keyLabel!)) { final int keyId = LogicalKeyboardKey.unicodePlane | (unicodeScalarValues & LogicalKeyboardKey.valueMask); return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey( keyId, keyLabel: keyLabel, - debugName: kReleaseMode ? null : 'Key ${keyLabel.toUpperCase()}', + debugName: kReleaseMode ? null : 'Key ${keyLabel!.toUpperCase()}', ); } // Look to see if the keyCode is one we know about and have a mapping for. - LogicalKeyboardKey newKey = keyHelper.logicalKey(keyCode); + LogicalKeyboardKey? newKey = keyHelper.logicalKey(keyCode); if (newKey != null) { return newKey; } @@ -158,13 +157,13 @@ abstract class KeyHelper { /// Returns true if the given [ModifierKey] was pressed at the time of this /// event. - bool isModifierPressed(ModifierKey key, int modifiers, {KeyboardSide side = KeyboardSide.any, int keyCode, bool isDown}); + bool isModifierPressed(ModifierKey key, int modifiers, {KeyboardSide side = KeyboardSide.any, required int keyCode, required bool isDown}); /// The numpad key from the specific key code mapping. - LogicalKeyboardKey numpadKey(int keyCode); + LogicalKeyboardKey? numpadKey(int keyCode); /// The logical key key from the specific key code mapping. - LogicalKeyboardKey logicalKey(int keyCode); + LogicalKeyboardKey? logicalKey(int keyCode); } /// Helper class that uses GLFW-specific key mappings. @@ -212,7 +211,7 @@ class GLFWKeyHelper with KeyHelper { /// {@macro flutter.services.glfwKeyHelper.modifiers} static const int modifierNumericPad = 0x0020; - int _mergeModifiers({int modifiers, int keyCode, bool isDown}) { + int _mergeModifiers({required int modifiers, required int keyCode, required bool isDown}) { // GLFW Key codes for modifier keys. const int shiftLeftKeyCode = 340; const int shiftRightKeyCode = 344; @@ -262,7 +261,7 @@ class GLFWKeyHelper with KeyHelper { } @override - bool isModifierPressed(ModifierKey key, int modifiers, {KeyboardSide side = KeyboardSide.any, int keyCode, bool isDown}) { + bool isModifierPressed(ModifierKey key, int modifiers, {KeyboardSide side = KeyboardSide.any, required int keyCode, required bool isDown}) { modifiers = _mergeModifiers(modifiers: modifiers, keyCode: keyCode, isDown: isDown); switch (key) { case ModifierKey.controlModifier: @@ -283,7 +282,6 @@ class GLFWKeyHelper with KeyHelper { // These are not used in GLFW keyboards. return false; } - return false; } @override @@ -303,17 +301,15 @@ class GLFWKeyHelper with KeyHelper { case ModifierKey.scrollLockModifier: return KeyboardSide.all; } - assert(false, 'Not handling $key type properly.'); - return null; } @override - LogicalKeyboardKey numpadKey(int keyCode) { + LogicalKeyboardKey? numpadKey(int keyCode) { return kGlfwNumpadMap[keyCode]; } @override - LogicalKeyboardKey logicalKey(int keyCode) { + LogicalKeyboardKey? logicalKey(int keyCode) { return kGlfwToLogicalKey[keyCode]; } } @@ -362,7 +358,7 @@ class GtkKeyHelper with KeyHelper { /// {@macro flutter.services.gtkKeyHelper.modifiers} static const int modifierMeta = 1 << 28; - int _mergeModifiers({int modifiers, int keyCode, bool isDown}) { + int _mergeModifiers({required int modifiers, required int keyCode, required bool isDown}) { // GTK Key codes for modifier keys. const int shiftLeftKeyCode = 0xffe1; const int shiftRightKeyCode = 0xffe2; @@ -414,7 +410,7 @@ class GtkKeyHelper with KeyHelper { } @override - bool isModifierPressed(ModifierKey key, int modifiers, {KeyboardSide side = KeyboardSide.any, int keyCode, bool isDown}) { + bool isModifierPressed(ModifierKey key, int modifiers, {KeyboardSide side = KeyboardSide.any, required int keyCode, required bool isDown}) { modifiers = _mergeModifiers(modifiers: modifiers, keyCode: keyCode, isDown: isDown); switch (key) { case ModifierKey.controlModifier: @@ -435,7 +431,6 @@ class GtkKeyHelper with KeyHelper { // These are not used in GTK keyboards. return false; } - return false; } @override @@ -455,17 +450,15 @@ class GtkKeyHelper with KeyHelper { case ModifierKey.scrollLockModifier: return KeyboardSide.all; } - assert(false, 'Not handling $key type properly.'); - return null; } @override - LogicalKeyboardKey numpadKey(int keyCode) { + LogicalKeyboardKey? numpadKey(int keyCode) { return kGtkNumpadMap[keyCode]; } @override - LogicalKeyboardKey logicalKey(int keyCode) { + LogicalKeyboardKey? logicalKey(int keyCode) { return kGtkToLogicalKey[keyCode]; } } diff --git a/packages/flutter/lib/src/services/raw_keyboard_macos.dart b/packages/flutter/lib/src/services/raw_keyboard_macos.dart index e6254360cf..d6c98abe71 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_macos.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_macos.dart @@ -2,7 +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:flutter/foundation.dart'; @@ -63,7 +62,7 @@ class RawKeyEventDataMacOs extends RawKeyEventData { final int modifiers; @override - String get keyLabel => charactersIgnoringModifiers.isEmpty ? null : charactersIgnoringModifiers; + String? get keyLabel => charactersIgnoringModifiers.isEmpty ? null : charactersIgnoringModifiers; @override PhysicalKeyboardKey get physicalKey => kMacOsToPhysicalKey[keyCode] ?? PhysicalKeyboardKey.none; @@ -73,7 +72,7 @@ class RawKeyEventDataMacOs extends RawKeyEventData { // Look to see if the keyCode is a printable number pad key, so that a // difference between regular keys (e.g. "=") and the number pad version // (e.g. the "=" on the number pad) can be determined. - final LogicalKeyboardKey numPadKey = kMacOsNumPadMap[keyCode]; + final LogicalKeyboardKey? numPadKey = kMacOsNumPadMap[keyCode]; if (numPadKey != null) { return numPadKey; } @@ -81,8 +80,8 @@ class RawKeyEventDataMacOs extends RawKeyEventData { // Control keys such as ESC, CRTL, and SHIFT are not printable. HOME, DEL, arrow keys, and function // keys are considered modifier function keys, which generate invalid Unicode scalar values. if (keyLabel != null && - !LogicalKeyboardKey.isControlCharacter(keyLabel) && - !_isUnprintableKey(keyLabel)) { + !LogicalKeyboardKey.isControlCharacter(keyLabel!) && + !_isUnprintableKey(keyLabel!)) { // Given that charactersIgnoringModifiers can contain a String of arbitrary length, // limit to a maximum of two Unicode scalar values. It is unlikely that a keyboard would produce a code point // bigger than 32 bits, but it is still worth defending against this case. @@ -97,7 +96,7 @@ class RawKeyEventDataMacOs extends RawKeyEventData { return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey( keyId, keyLabel: keyLabel, - debugName: kReleaseMode ? null : 'Key ${keyLabel.toUpperCase()}', + debugName: kReleaseMode ? null : 'Key ${keyLabel!.toUpperCase()}', ); } @@ -145,7 +144,6 @@ class RawKeyEventDataMacOs extends RawKeyEventData { case KeyboardSide.right: return modifiers & rightMask != 0 || anyOnly; } - return false; } @override @@ -185,8 +183,8 @@ class RawKeyEventDataMacOs extends RawKeyEventData { } @override - KeyboardSide getModifierSide(ModifierKey key) { - KeyboardSide findSide(int leftMask, int rightMask, int anyMask) { + KeyboardSide? getModifierSide(ModifierKey key) { + KeyboardSide? findSide(int leftMask, int rightMask, int anyMask) { final int combinedMask = leftMask | rightMask; final int combined = modifiers & combinedMask; if (combined == leftMask) { @@ -218,9 +216,6 @@ class RawKeyEventDataMacOs extends RawKeyEventData { case ModifierKey.symbolModifier: return KeyboardSide.all; } - - assert(false, 'Not handling $key type properly.'); - return null; } /// Returns true if the given label represents an unprintable key. diff --git a/packages/flutter/lib/src/services/raw_keyboard_web.dart b/packages/flutter/lib/src/services/raw_keyboard_web.dart index e0f96ff85e..c933b7bade 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_web.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_web.dart @@ -2,7 +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:flutter/foundation.dart'; @@ -21,8 +20,8 @@ class RawKeyEventDataWeb extends RawKeyEventData { /// /// The [code] and [metaState] arguments must not be null. const RawKeyEventDataWeb({ - @required this.code, - @required this.key, + required this.code, + required this.key, this.metaState = modifierNone, }) : assert(code != null), assert(metaState != null); @@ -69,13 +68,13 @@ class RawKeyEventDataWeb extends RawKeyEventData { // Look to see if the keyCode is a printable number pad key, so that a // difference between regular keys (e.g. ".") and the number pad version // (e.g. the "." on the number pad) can be determined. - final LogicalKeyboardKey numPadKey = kWebNumPadMap[code]; + final LogicalKeyboardKey? numPadKey = kWebNumPadMap[code]; if (numPadKey != null) { return numPadKey; } // Look to see if the [code] is one we know about and have a mapping for. - final LogicalKeyboardKey newKey = kWebToLogicalKey[code]; + final LogicalKeyboardKey? newKey = kWebToLogicalKey[code]; if (newKey != null) { return newKey; } diff --git a/packages/flutter/lib/src/services/raw_keyboard_windows.dart b/packages/flutter/lib/src/services/raw_keyboard_windows.dart index c1163a0b88..1f943cf7b0 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_windows.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_windows.dart @@ -2,7 +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:flutter/foundation.dart'; @@ -55,7 +54,7 @@ class RawKeyEventDataWindows extends RawKeyEventData { final int modifiers; @override - String get keyLabel => characterCodePoint == 0 ? null : String.fromCharCode(characterCodePoint); + String? get keyLabel => characterCodePoint == 0 ? null : String.fromCharCode(characterCodePoint); @override PhysicalKeyboardKey get physicalKey => kWindowsToPhysicalKey[scanCode] ?? PhysicalKeyboardKey.none; @@ -65,7 +64,7 @@ class RawKeyEventDataWindows extends RawKeyEventData { // Look to see if the keyCode is a printable number pad key, so that a // difference between regular keys (e.g. "=") and the number pad version // (e.g. the "=" on the number pad) can be determined. - final LogicalKeyboardKey numPadKey = kWindowsNumPadMap[keyCode]; + final LogicalKeyboardKey? numPadKey = kWindowsNumPadMap[keyCode]; if (numPadKey != null) { return numPadKey; } @@ -74,16 +73,16 @@ class RawKeyEventDataWindows extends RawKeyEventData { // constant, or construct a new Unicode-based key from it. Don't mark it as // autogenerated, since the label uniquely identifies an ID from the Unicode // plane. - if (keyLabel != null && keyLabel.isNotEmpty && !LogicalKeyboardKey.isControlCharacter(keyLabel)) { + if (keyLabel != null && keyLabel!.isNotEmpty && !LogicalKeyboardKey.isControlCharacter(keyLabel!)) { final int keyId = LogicalKeyboardKey.unicodePlane | (characterCodePoint & LogicalKeyboardKey.valueMask); return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey( keyId, keyLabel: keyLabel, - debugName: kReleaseMode ? null : 'Key ${keyLabel.toUpperCase()}', + debugName: kReleaseMode ? null : 'Key ${keyLabel!.toUpperCase()}', ); } // Look to see if the keyCode is one we know about and have a mapping for. - LogicalKeyboardKey newKey = kWindowsToLogicalKey[keyCode]; + LogicalKeyboardKey? newKey = kWindowsToLogicalKey[keyCode]; if (newKey != null) { return newKey; } @@ -120,7 +119,6 @@ class RawKeyEventDataWindows extends RawKeyEventData { case KeyboardSide.right: return modifiers & rightMask != 0 || anyOnly; } - return false; } @override @@ -162,8 +160,8 @@ class RawKeyEventDataWindows extends RawKeyEventData { @override - KeyboardSide getModifierSide(ModifierKey key) { - KeyboardSide findSide(int leftMask, int rightMask, int anyMask) { + KeyboardSide? getModifierSide(ModifierKey key) { + KeyboardSide? findSide(int leftMask, int rightMask, int anyMask) { final int combinedMask = leftMask | rightMask; final int combined = modifiers & combinedMask; if (combined == leftMask) { @@ -197,9 +195,6 @@ class RawKeyEventDataWindows extends RawKeyEventData { case ModifierKey.symbolModifier: return KeyboardSide.all; } - - assert(false, 'Not handling $key type properly.'); - return null; } // These are not the values defined by the Windows header for each modifier. Since they diff --git a/packages/flutter/lib/src/services/restoration.dart b/packages/flutter/lib/src/services/restoration.dart index 1997a8412c..2184285c45 100644 --- a/packages/flutter/lib/src/services/restoration.dart +++ b/packages/flutter/lib/src/services/restoration.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:typed_data'; @@ -132,25 +130,25 @@ class RestorationManager extends ChangeNotifier { /// /// * [RootRestorationScope], which makes the root bucket available in the /// [Widget] tree. - Future get rootBucket { + Future get rootBucket { if (!SystemChannels.restoration.checkMethodCallHandler(_methodHandler)) { SystemChannels.restoration.setMethodCallHandler(_methodHandler); } if (_rootBucketIsValid) { - return SynchronousFuture(_rootBucket); + return SynchronousFuture(_rootBucket); } if (_pendingRootBucket == null) { _pendingRootBucket = Completer(); _getRootBucketFromEngine(); } - return _pendingRootBucket.future; + return _pendingRootBucket!.future; } - RestorationBucket _rootBucket; // May be null to indicate that restoration is turned off. - Completer _pendingRootBucket; + RestorationBucket? _rootBucket; // May be null to indicate that restoration is turned off. + Completer? _pendingRootBucket; bool _rootBucketIsValid = false; Future _getRootBucketFromEngine() async { - final Map config = await SystemChannels.restoration.invokeMethod>('get'); + final Map? config = await SystemChannels.restoration.invokeMethod>('get'); if (_pendingRootBucket == null) { // The restoration data was obtained via other means (e.g. by calling // [handleRestorationDataUpdate] while the request to the engine was @@ -161,7 +159,7 @@ class RestorationManager extends ChangeNotifier { _parseAndHandleRestorationUpdateFromEngine(config); } - void _parseAndHandleRestorationUpdateFromEngine(Map update) { + void _parseAndHandleRestorationUpdateFromEngine(Map? update) { handleRestorationUpdateFromEngine( enabled: update != null && update['enabled'] as bool, data: update == null ? null : update['data'] as Uint8List, @@ -185,16 +183,16 @@ class RestorationManager extends ChangeNotifier { /// accessed, [rootBucket] will complete synchronously the next time it is /// called. @protected - void handleRestorationUpdateFromEngine({@required bool enabled, @required Uint8List data}) { + void handleRestorationUpdateFromEngine({required bool enabled, required Uint8List? data}) { assert(enabled != null); - final RestorationBucket oldRoot = _rootBucket; + final RestorationBucket? oldRoot = _rootBucket; _rootBucket = enabled ? RestorationBucket.root(manager: this, rawData: _decodeRestorationData(data)) : null; _rootBucketIsValid = true; - assert(_pendingRootBucket == null || !_pendingRootBucket.isCompleted); + assert(_pendingRootBucket == null || !_pendingRootBucket!.isCompleted); _pendingRootBucket?.complete(_rootBucket); _pendingRootBucket = null; @@ -228,7 +226,7 @@ class RestorationManager extends ChangeNotifier { ); } - Future _methodHandler(MethodCall call) { + Future _methodHandler(MethodCall call) async { switch (call.method) { case 'push': _parseAndHandleRestorationUpdateFromEngine(call.arguments as Map); @@ -236,10 +234,9 @@ class RestorationManager extends ChangeNotifier { default: throw UnimplementedError("${call.method} was invoked but isn't implemented by $runtimeType"); } - return null; } - Map _decodeRestorationData(Uint8List data) { + Map? _decodeRestorationData(Uint8List? data) { if (data == null) { return null; } @@ -248,7 +245,7 @@ class RestorationManager extends ChangeNotifier { } Uint8List _encodeRestorationData(Map data) { - final ByteData encoded = const StandardMessageCodec().encodeMessage(data); + final ByteData encoded = const StandardMessageCodec().encodeMessage(data)!; return encoded.buffer.asUint8List(encoded.offsetInBytes, encoded.lengthInBytes); } @@ -275,7 +272,7 @@ class RestorationManager extends ChangeNotifier { _bucketsNeedingSerialization.add(bucket); if (!_postFrameScheduled) { _postFrameScheduled = true; - SchedulerBinding.instance.addPostFrameCallback((Duration _) => _doSerialization()); + SchedulerBinding.instance!.addPostFrameCallback((Duration _) => _doSerialization()); } } @@ -308,7 +305,7 @@ class RestorationManager extends ChangeNotifier { bucket.finalize(); } _bucketsNeedingSerialization.clear(); - sendToEngine(_encodeRestorationData(_rootBucket._rawData)); + sendToEngine(_encodeRestorationData(_rootBucket!._rawData)); assert(() { _debugDoingUpdate = false; @@ -410,8 +407,8 @@ class RestorationBucket extends ChangeNotifier { /// /// The `restorationId` must not be null. RestorationBucket.empty({ - @required String restorationId, - @required Object debugOwner, + required String restorationId, + required Object? debugOwner, }) : assert(restorationId != null), _restorationId = restorationId, _rawData = {} { @@ -445,8 +442,8 @@ class RestorationBucket extends ChangeNotifier { /// /// The `manager` argument must not be null. RestorationBucket.root({ - @required RestorationManager manager, - @required Map rawData, + required RestorationManager manager, + required Map? rawData, }) : assert(manager != null), _manager = manager, _rawData = rawData ?? {}, @@ -468,9 +465,9 @@ class RestorationBucket extends ChangeNotifier { /// /// The `restorationId` and `parent` argument must not be null. RestorationBucket.child({ - @required String restorationId, - @required RestorationBucket parent, - @required Object debugOwner, + required String restorationId, + required RestorationBucket parent, + required Object? debugOwner, }) : assert(restorationId != null), assert(parent != null), assert(parent._rawChildren[restorationId] != null), @@ -494,14 +491,14 @@ class RestorationBucket extends ChangeNotifier { /// /// The value is used in error messages. Accessing the value is only valid /// in debug mode, otherwise it will return null. - Object get debugOwner { + Object? get debugOwner { assert(_debugAssertNotDisposed()); return _debugOwner; } - Object _debugOwner; + Object? _debugOwner; - RestorationManager _manager; - RestorationBucket _parent; + RestorationManager? _manager; + RestorationBucket? _parent; /// The restoration ID under which the bucket is currently stored in the /// parent of this bucket (or wants to be stored if it is currently @@ -545,7 +542,7 @@ class RestorationBucket extends ChangeNotifier { void decommission() { assert(_debugAssertNotDisposed()); if (_parent != null) { - _parent._dropChild(this); + _parent!._dropChild(this); _parent = null; } _performDecommission(); @@ -678,7 +675,7 @@ class RestorationBucket extends ChangeNotifier { /// /// When the returned bucket is no longer needed, it must be [dispose]d to /// delete the information stored in it from the app's restoration data. - RestorationBucket claimChild(String restorationId, {@required Object debugOwner}) { + RestorationBucket claimChild(String restorationId, {required Object? debugOwner}) { assert(_debugAssertNotDisposed()); assert(restorationId != null); // There are three cases to consider: @@ -780,7 +777,7 @@ class RestorationBucket extends ChangeNotifier { bucket._visitChildren(_recursivelyUpdateManager); } - void _updateManager(RestorationManager newManager) { + void _updateManager(RestorationManager? newManager) { if (_manager == newManager) { return; } @@ -811,7 +808,7 @@ class RestorationBucket extends ChangeNotifier { error.addAll([ ErrorDescription(' * "$id" was claimed by:'), ...buckets.map((RestorationBucket bucket) => ErrorDescription(' * ${bucket.debugOwner}')), - ErrorDescription(' * ${_claimedChildren[id].debugOwner} (current owner)'), + ErrorDescription(' * ${_claimedChildren[id]!.debugOwner} (current owner)'), ]); } throw FlutterError.fromParts(error); @@ -824,7 +821,7 @@ class RestorationBucket extends ChangeNotifier { assert(child._parent == this); if (_claimedChildren.remove(child.restorationId) == child) { _rawChildren.remove(child.restorationId); - final List pendingChildren = _childrenToAdd[child.restorationId]; + final List? pendingChildren = _childrenToAdd[child.restorationId]; if (pendingChildren != null) { final RestorationBucket toAdd = pendingChildren.removeLast(); _finalizeAddChildData(toAdd); @@ -893,9 +890,9 @@ class RestorationBucket extends ChangeNotifier { if (newRestorationId == restorationId) { return; } - _parent._removeChildData(this); + _parent!._removeChildData(this); _restorationId = newRestorationId; - _parent._addChildData(this); + _parent!._addChildData(this); } /// Deletes the bucket and all the data stored in it from the bucket @@ -946,7 +943,7 @@ class RestorationBucket extends ChangeNotifier { /// /// Should only be called from within asserts. Always returns false outside /// of debug builds. -bool debugIsSerializableForRestoration(Object object) { +bool debugIsSerializableForRestoration(Object? object) { bool result = false; assert(() { diff --git a/packages/flutter/lib/src/services/system_channels.dart b/packages/flutter/lib/src/services/system_channels.dart index aa0dbad8b6..3a17e21338 100644 --- a/packages/flutter/lib/src/services/system_channels.dart +++ b/packages/flutter/lib/src/services/system_channels.dart @@ -2,7 +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:ui'; @@ -218,7 +217,7 @@ class SystemChannels { /// /// * [WidgetsBindingObserver.didChangeAppLifecycleState], which triggers /// whenever a message is received on this channel. - static const BasicMessageChannel lifecycle = BasicMessageChannel( + static const BasicMessageChannel lifecycle = BasicMessageChannel( 'flutter/lifecycle', StringCodec(), ); diff --git a/packages/flutter/lib/src/services/system_chrome.dart b/packages/flutter/lib/src/services/system_chrome.dart index a1f8199b44..40809b9c0d 100644 --- a/packages/flutter/lib/src/services/system_chrome.dart +++ b/packages/flutter/lib/src/services/system_chrome.dart @@ -2,7 +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:ui'; @@ -60,13 +59,13 @@ class ApplicationSwitcherDescription { const ApplicationSwitcherDescription({ this.label, this.primaryColor }); /// A label and description of the current state of the application. - final String label; + final String? label; /// The application's primary color. /// /// This may influence the color that the operating system uses to represent /// the application. - final int primaryColor; + final int? primaryColor; } /// Specifies a system overlay at a particular location. @@ -100,32 +99,32 @@ class SystemUiOverlayStyle { /// The color of the system bottom navigation bar. /// /// Only honored in Android versions O and greater. - final Color systemNavigationBarColor; + final Color? systemNavigationBarColor; /// The color of the divider between the system's bottom navigation bar and the app's content. /// /// Only honored in Android versions P and greater. - final Color systemNavigationBarDividerColor; + final Color? systemNavigationBarDividerColor; /// The brightness of the system navigation bar icons. /// /// Only honored in Android versions O and greater. - final Brightness systemNavigationBarIconBrightness; + final Brightness? systemNavigationBarIconBrightness; /// The color of top status bar. /// /// Only honored in Android version M and greater. - final Color statusBarColor; + final Color? statusBarColor; /// The brightness of top status bar. /// /// Only honored in iOS. - final Brightness statusBarBrightness; + final Brightness? statusBarBrightness; /// The brightness of the top status bar icons. /// /// Only honored in Android version M and greater. - final Brightness statusBarIconBrightness; + final Brightness? statusBarIconBrightness; /// System overlays should be drawn with a light color. Intended for /// applications with a dark background. @@ -166,12 +165,12 @@ class SystemUiOverlayStyle { /// Creates a copy of this theme with the given fields replaced with new values. SystemUiOverlayStyle copyWith({ - Color systemNavigationBarColor, - Color systemNavigationBarDividerColor, - Color statusBarColor, - Brightness statusBarBrightness, - Brightness statusBarIconBrightness, - Brightness systemNavigationBarIconBrightness, + Color? systemNavigationBarColor, + Color? systemNavigationBarDividerColor, + Color? statusBarColor, + Brightness? statusBarBrightness, + Brightness? statusBarIconBrightness, + Brightness? systemNavigationBarIconBrightness, }) { return SystemUiOverlayStyle( systemNavigationBarColor: systemNavigationBarColor ?? this.systemNavigationBarColor, @@ -406,7 +405,7 @@ class SystemChrome { if (_pendingStyle != _latestStyle) { SystemChannels.platform.invokeMethod( 'SystemChrome.setSystemUIOverlayStyle', - _pendingStyle._toMap(), + _pendingStyle!._toMap(), ); _latestStyle = _pendingStyle; } @@ -414,10 +413,10 @@ class SystemChrome { }); } - static SystemUiOverlayStyle _pendingStyle; + static SystemUiOverlayStyle? _pendingStyle; /// The last style that was set using [SystemChrome.setSystemUIOverlayStyle]. @visibleForTesting - static SystemUiOverlayStyle get latestStyle => _latestStyle; - static SystemUiOverlayStyle _latestStyle; + static SystemUiOverlayStyle? get latestStyle => _latestStyle; + static SystemUiOverlayStyle? _latestStyle; } diff --git a/packages/flutter/lib/src/services/system_navigator.dart b/packages/flutter/lib/src/services/system_navigator.dart index f61ce7b2e9..8efdfef614 100644 --- a/packages/flutter/lib/src/services/system_navigator.dart +++ b/packages/flutter/lib/src/services/system_navigator.dart @@ -2,7 +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'; @@ -33,7 +32,7 @@ class SystemNavigator { /// This method should be preferred over calling `dart:io`'s [exit] /// method, as the latter may cause the underlying platform to act /// as if the application had crashed. - static Future pop({bool animated}) async { + static Future pop({bool? animated}) async { await SystemChannels.platform.invokeMethod('SystemNavigator.pop', animated); } } diff --git a/packages/flutter/lib/src/services/system_sound.dart b/packages/flutter/lib/src/services/system_sound.dart index 64ce10437b..b1cc510504 100644 --- a/packages/flutter/lib/src/services/system_sound.dart +++ b/packages/flutter/lib/src/services/system_sound.dart @@ -2,7 +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'; diff --git a/packages/flutter/lib/src/services/text_editing.dart b/packages/flutter/lib/src/services/text_editing.dart index f770f7034a..32c1c49467 100644 --- a/packages/flutter/lib/src/services/text_editing.dart +++ b/packages/flutter/lib/src/services/text_editing.dart @@ -2,7 +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:ui' show hashValues, TextAffinity, TextPosition, TextRange; @@ -17,8 +16,8 @@ class TextSelection extends TextRange { /// /// The [baseOffset] and [extentOffset] arguments must not be null. const TextSelection({ - @required this.baseOffset, - @required this.extentOffset, + required this.baseOffset, + required this.extentOffset, this.affinity = TextAffinity.downstream, this.isDirectional = false, }) : super( @@ -34,7 +33,7 @@ class TextSelection extends TextRange { /// /// The [offset] argument must not be null. const TextSelection.collapsed({ - @required int offset, + required int offset, this.affinity = TextAffinity.downstream, }) : baseOffset = offset, extentOffset = offset, @@ -121,10 +120,10 @@ class TextSelection extends TextRange { /// Creates a new [TextSelection] based on the current selection, with the /// provided parameters overridden. TextSelection copyWith({ - int baseOffset, - int extentOffset, - TextAffinity affinity, - bool isDirectional, + int? baseOffset, + int? extentOffset, + TextAffinity? affinity, + bool? isDirectional, }) { return TextSelection( baseOffset: baseOffset ?? this.baseOffset, diff --git a/packages/flutter/lib/src/services/text_formatter.dart b/packages/flutter/lib/src/services/text_formatter.dart index 0f6a2867e5..d403b18746 100644 --- a/packages/flutter/lib/src/services/text_formatter.dart +++ b/packages/flutter/lib/src/services/text_formatter.dart @@ -2,7 +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:math' as math; @@ -103,7 +102,7 @@ class FilteringTextInputFormatter extends TextInputFormatter { /// must not be null. FilteringTextInputFormatter( this.filterPattern, { - @required this.allow, + required this.allow, this.replacementString = '', }) : assert(filterPattern != null), assert(allow != null), @@ -342,7 +341,7 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter { /// counted as a single character, but because it is a combination of two /// Unicode scalar values, '\u{1F44D}\u{1F3FD}', it is counted as two /// characters. - final int maxLength; + final int? maxLength; /// Truncate the given TextEditingValue to maxLength characters. /// @@ -371,13 +370,13 @@ class LengthLimitingTextInputFormatter extends TextInputFormatter { TextEditingValue oldValue, // unused. TextEditingValue newValue, ) { - if (maxLength != null && maxLength > 0 && newValue.text.characters.length > maxLength) { + if (maxLength != null && maxLength! > 0 && newValue.text.characters.length > maxLength!) { // If already at the maximum and tried to enter even more, keep the old // value. if (oldValue.text.characters.length == maxLength) { return oldValue; } - return truncate(newValue, maxLength); + return truncate(newValue, maxLength!); } return newValue; } @@ -390,7 +389,7 @@ TextEditingValue _selectionAwareTextManipulation( final int selectionStartIndex = value.selection.start; final int selectionEndIndex = value.selection.end; String manipulatedText; - TextSelection manipulatedSelection; + TextSelection? manipulatedSelection; if (selectionStartIndex < 0 || selectionEndIndex < 0) { manipulatedText = substringManipulation(value.text); } else { diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index 3ec40d8999..d98009d090 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -2,7 +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:io' show Platform; @@ -101,13 +100,13 @@ class TextInputType { /// /// This flag is only used for the [number] input type, otherwise `null`. /// Use `const TextInputType.numberWithOptions(signed: true)` to set this. - final bool signed; + final bool? signed; /// The number is decimal, allowing a decimal point to provide fractional. /// /// This flag is only used for the [number] input type, otherwise `null`. /// Use `const TextInputType.numberWithOptions(decimal: true)` to set this. - final bool decimal; + final bool? decimal; /// Optimize for textual information. /// @@ -450,8 +449,8 @@ class TextInputConfiguration { this.inputType = TextInputType.text, this.obscureText = false, this.autocorrect = true, - SmartDashesType smartDashesType, - SmartQuotesType smartQuotesType, + SmartDashesType? smartDashesType, + SmartQuotesType? smartQuotesType, this.enableSuggestions = true, this.actionLabel, this.inputAction = TextInputAction.done, @@ -487,7 +486,7 @@ class TextInputConfiguration { /// to the platform. This will prevent the corresponding input field from /// participating in autofills triggered by other fields. Additionally, on /// Android and web, setting [autofillConfiguration] to null disables autofill. - final AutofillConfiguration autofillConfiguration; + final AutofillConfiguration? autofillConfiguration; /// {@template flutter.services.textInput.smartDashesType} /// Whether to allow the platform to automatically format dashes. @@ -555,7 +554,7 @@ class TextInputConfiguration { final bool enableSuggestions; /// What text to display in the text input control's action button. - final String actionLabel; + final String? actionLabel; /// What kind of action to request for the action button on the IME. final TextInputAction inputAction; @@ -590,12 +589,12 @@ class TextInputConfiguration { 'inputAction': inputAction.toString(), 'textCapitalization': textCapitalization.toString(), 'keyboardAppearance': keyboardAppearance.toString(), - if (autofillConfiguration != null) 'autofill': autofillConfiguration.toJson(), + if (autofillConfiguration != null) 'autofill': autofillConfiguration!.toJson(), }; } } -TextAffinity _toTextAffinity(String affinity) { +TextAffinity? _toTextAffinity(String affinity) { switch (affinity) { case 'TextAffinity.downstream': return TextAffinity.downstream; @@ -628,12 +627,12 @@ class RawFloatingCursorPoint { /// [FloatingCursorDragState.Update]. RawFloatingCursorPoint({ this.offset, - @required this.state, + required this.state, }) : assert(state != null), assert(state != FloatingCursorDragState.Update || offset != null); /// The raw position of the floating cursor as determined by the iOS sdk. - final Offset offset; + final Offset? offset; /// The state of the floating cursor. final FloatingCursorDragState state; @@ -661,14 +660,14 @@ class TextEditingValue { return TextEditingValue( text: encoded['text'] as String, selection: TextSelection( - baseOffset: encoded['selectionBase'] as int ?? -1, - extentOffset: encoded['selectionExtent'] as int ?? -1, + baseOffset: encoded['selectionBase'] as int? ?? -1, + extentOffset: encoded['selectionExtent'] as int? ?? -1, affinity: _toTextAffinity(encoded['selectionAffinity'] as String) ?? TextAffinity.downstream, - isDirectional: encoded['selectionIsDirectional'] as bool ?? false, + isDirectional: encoded['selectionIsDirectional'] as bool? ?? false, ), composing: TextRange( - start: encoded['composingBase'] as int ?? -1, - end: encoded['composingExtent'] as int ?? -1, + start: encoded['composingBase'] as int? ?? -1, + end: encoded['composingExtent'] as int? ?? -1, ), ); } @@ -700,9 +699,9 @@ class TextEditingValue { /// Creates a copy of this value but with the given fields replaced with the new values. TextEditingValue copyWith({ - String text, - TextSelection selection, - TextRange composing, + String? text, + TextSelection? selection, + TextRange? composing, }) { return TextEditingValue( text: text ?? this.text, @@ -795,7 +794,7 @@ abstract class TextInputClient { /// /// * [AutofillGroup], a widget that creates an [AutofillScope] for its /// descendent autofillable [TextInputClient]s. - AutofillScope get currentAutofillScope; + AutofillScope? get currentAutofillScope; /// Requests that this client update its editing state to the given value. void updateEditingValue(TextEditingValue value); @@ -828,8 +827,8 @@ class TextInputConnection { : assert(_client != null), _id = _nextId++; - Size _cachedSize; - Matrix4 _cachedTransform; + Size? _cachedSize; + Matrix4? _cachedTransform; static int _nextId = 1; final int _id; @@ -906,11 +905,11 @@ class TextInputConnection { /// of the hidden native input's content. Hence, the content size will match /// to the size of the editable widget's content. void setStyle({ - @required String fontFamily, - @required double fontSize, - @required FontWeight fontWeight, - @required TextDirection textDirection, - @required TextAlign textAlign, + required String? fontFamily, + required double? fontSize, + required FontWeight? fontWeight, + required TextDirection textDirection, + required TextAlign textAlign, }) { assert(attached); @@ -1108,10 +1107,10 @@ class TextInput { return true; } - MethodChannel _channel; + late MethodChannel _channel; - TextInputConnection _currentConnection; - TextInputConfiguration _currentConfiguration; + TextInputConnection? _currentConnection; + late TextInputConfiguration _currentConfiguration; Future _handleTextInputInvocation(MethodCall methodCall) async { if (_currentConnection == null) @@ -1121,9 +1120,9 @@ class TextInput { // The requestExistingInputState request needs to be handled regardless of // the client ID, as long as we have a _currentConnection. if (method == 'TextInputClient.requestExistingInputState') { - assert(_currentConnection._client != null); - _attach(_currentConnection, _currentConfiguration); - final TextEditingValue editingValue = _currentConnection._client.currentTextEditingValue; + assert(_currentConnection!._client != null); + _attach(_currentConnection!, _currentConfiguration); + final TextEditingValue editingValue = _currentConnection!._client.currentTextEditingValue; if (editingValue != null) { _setEditingState(editingValue); } @@ -1133,9 +1132,9 @@ class TextInput { final List args = methodCall.arguments as List; if (method == 'TextInputClient.updateEditingStateWithTag') { - final TextInputClient client = _currentConnection._client; + final TextInputClient client = _currentConnection!._client; assert(client != null); - final AutofillScope scope = client.currentAutofillScope; + final AutofillScope? scope = client.currentAutofillScope; final Map editingValue = args[1] as Map; for (final String tag in editingValue.keys) { final TextEditingValue textEditingValue = TextEditingValue.fromJSON( @@ -1149,26 +1148,26 @@ class TextInput { final int client = args[0] as int; // The incoming message was for a different client. - if (client != _currentConnection._id) + if (client != _currentConnection!._id) return; switch (method) { case 'TextInputClient.updateEditingState': - _currentConnection._client.updateEditingValue(TextEditingValue.fromJSON(args[1] as Map)); + _currentConnection!._client.updateEditingValue(TextEditingValue.fromJSON(args[1] as Map)); break; case 'TextInputClient.performAction': - _currentConnection._client.performAction(_toTextInputAction(args[1] as String)); + _currentConnection!._client.performAction(_toTextInputAction(args[1] as String)); break; case 'TextInputClient.updateFloatingCursor': - _currentConnection._client.updateFloatingCursor(_toTextPoint( + _currentConnection!._client.updateFloatingCursor(_toTextPoint( _toTextCursorAction(args[1] as String), args[2] as Map, )); break; case 'TextInputClient.onConnectionClosed': - _currentConnection._client.connectionClosed(); + _currentConnection!._client.connectionClosed(); break; case 'TextInputClient.showAutocorrectionPromptRect': - _currentConnection._client.showAutocorrectionPromptRect(args[1] as int, args[2] as int); + _currentConnection!._client.showAutocorrectionPromptRect(args[1] as int, args[2] as int); break; default: throw MissingPluginException();