Skip to content

Conversation

@mit-mit
Copy link
Member

@mit-mit mit-mit commented Mar 19, 2021

Enable Dart null safety (https://dart.dev/null-safety) by default in all flutter create templates.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Mar 19, 2021
@google-cla google-cla bot added the cla: yes label Mar 19, 2021
@mit-mit
Copy link
Member Author

mit-mit commented Mar 19, 2021

@munificent can you check to see if my changes are ideomatic?

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look like some tool tests are failing.


static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
static Future<String?> get platformVersion async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this actually be allowed to return null? Or is null always an error case (e.g. plugin wasn't available).

Copy link
Member Author

@mit-mit mit-mit Mar 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading this correctly, then MethodChannel can never return null, but OptionalMethodChannel can.
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/services/platform_channel.dart#L330

I think we know in MethodChannel that T is never null (as per missingOk: false), so we could change MethodChannel.invokeMethod to return Future<T> not Future<T?>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work:

diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart
index c9bad0e210..bd07c68829 100644
--- a/packages/flutter/lib/src/services/platform_channel.dart
+++ b/packages/flutter/lib/src/services/platform_channel.dart
@@ -327,8 +327,8 @@ class MethodChannel {
   ///  * <https://api.flutter.dev/javadoc/io/flutter/plugin/common/MethodCall.html>
   ///    for how to access method call arguments on Android.
   @optionalTypeArgs
-  Future<T?> invokeMethod<T>(String method, [ dynamic arguments ]) {
-    return _invokeMethod<T>(method, missingOk: false, arguments: arguments);
+  Future<T> invokeMethod<T>(String method, [ dynamic arguments ]) {
+    return _invokeMethod<T>(method, missingOk: false, arguments: arguments) as T;
   }

Who know this code best and can comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goderbauer WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like changing the interface requires a larger refactoring...

@Hixie
Copy link
Contributor

Hixie commented Mar 19, 2021

cc @jonahwilliams

Copy link
Contributor

@munificent munificent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dart changes look idiomatic to me.

@mit-mit
Copy link
Member Author

mit-mit commented Mar 29, 2021

CI is now passing (the red X is the Flutter Dashboard being red).

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tool changes LGTM

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
static Future<String?> get platformVersion async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like changing the interface requires a larger refactoring...

Co-authored-by: Michael Goderbauer <goderbauer@google.com>
@mit-mit mit-mit merged commit 2cdd519 into flutter:master Mar 30, 2021
@mit-mit mit-mit deleted the nullsafetyintemplates branch March 30, 2021 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants