Skip to content

Enable null safety by default in templates - #78619

Merged
mit-mit merged 9 commits into
flutter:masterfrom
mit-mit:nullsafetyintemplates
Mar 30, 2021
Merged

mit-mit merged 9 commits into
flutter:masterfrom
mit-mit:nullsafetyintemplates

Conversation

@mit-mit

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

Copy link
Copy Markdown
Member

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

@flutter-dashboard

Copy link
Copy Markdown

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

mit-mit commented Mar 19, 2021

Copy link
Copy Markdown
Member Author

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

@goderbauer goderbauer left a comment

Copy link
Copy Markdown
Member

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.

Comment thread packages/flutter_tools/templates/app/lib/main.dart.tmpl Outdated
Comment thread packages/flutter_tools/templates/module/common/lib/main.dart.tmpl Outdated

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

Copy link
Copy Markdown
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).

@mit-mit mit-mit Mar 22, 2021 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Hixie commented Mar 19, 2021

Copy link
Copy Markdown
Contributor

cc @jonahwilliams

Comment thread packages/flutter_tools/lib/src/commands/create.dart

@munificent munificent left a comment

Copy link
Copy Markdown
Contributor

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

mit-mit commented Mar 29, 2021

Copy link
Copy Markdown
Member Author

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

@jonahwilliams jonahwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tool changes LGTM

@goderbauer goderbauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread dev/integration_tests/ios_host_app/flutterapp/lib/main Outdated

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

Copy link
Copy Markdown
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...

@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