From 4597be444039b09ccda2fc706d7ed7277c1bbdd2 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Mon, 12 Oct 2020 21:41:18 +0200 Subject: [PATCH 1/6] Migrate to null safety BREAKING CHANGE: this will introduce null safety and thus updates the signature of several methods. --- .../CHANGELOG.md | 4 ++++ .../lib/method_channel_url_launcher.dart | 18 +++++++++--------- .../lib/url_launcher_platform_interface.dart | 18 +++++++++--------- .../pubspec.yaml | 14 +++++++++----- .../test/method_channel_url_launcher_test.dart | 2 ++ 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/packages/url_launcher/url_launcher_platform_interface/CHANGELOG.md b/packages/url_launcher/url_launcher_platform_interface/CHANGELOG.md index 768042be4cef..f5bb44fb2423 100644 --- a/packages/url_launcher/url_launcher_platform_interface/CHANGELOG.md +++ b/packages/url_launcher/url_launcher_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.0-nullsafety + +* Migrate to null safety. + ## 1.0.8 * Added webOnlyWindowName parameter diff --git a/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart b/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart index f87630ee3045..7eb6b7ad23e3 100644 --- a/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart +++ b/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart @@ -14,7 +14,7 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher'); /// An implementation of [UrlLauncherPlatform] that uses method channels. class MethodChannelUrlLauncher extends UrlLauncherPlatform { @override - Future canLaunch(String url) { + Future canLaunch(String url) { return _channel.invokeMethod( 'canLaunch', {'url': url}, @@ -27,15 +27,15 @@ class MethodChannelUrlLauncher extends UrlLauncherPlatform { } @override - Future launch( + Future launch( String url, { - @required bool useSafariVC, - @required bool useWebView, - @required bool enableJavaScript, - @required bool enableDomStorage, - @required bool universalLinksOnly, - @required Map headers, - String webOnlyWindowName, + required bool useSafariVC, + required bool useWebView, + required bool enableJavaScript, + required bool enableDomStorage, + required bool universalLinksOnly, + required Map headers, + String? webOnlyWindowName, }) { return _channel.invokeMethod( 'launch', diff --git a/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart b/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart index 1de5742c1f6f..3c8d7dce642e 100644 --- a/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart +++ b/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart @@ -39,7 +39,7 @@ abstract class UrlLauncherPlatform extends PlatformInterface { } /// Returns `true` if this platform is able to launch [url]. - Future canLaunch(String url) { + Future canLaunch(String url) { throw UnimplementedError('canLaunch() has not been implemented.'); } @@ -47,15 +47,15 @@ abstract class UrlLauncherPlatform extends PlatformInterface { /// /// For documentation on the other arguments, see the `launch` documentation /// in `package:url_launcher/url_launcher.dart`. - Future launch( + Future launch( String url, { - @required bool useSafariVC, - @required bool useWebView, - @required bool enableJavaScript, - @required bool enableDomStorage, - @required bool universalLinksOnly, - @required Map headers, - String webOnlyWindowName, + required bool useSafariVC, + required bool useWebView, + required bool enableJavaScript, + required bool enableDomStorage, + required bool universalLinksOnly, + required Map headers, + String? webOnlyWindowName, }) { throw UnimplementedError('launch() has not been implemented.'); } diff --git a/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml b/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml index 0c4096278bcb..81aec6ac08e0 100644 --- a/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml +++ b/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml @@ -3,20 +3,24 @@ description: A common platform interface for the url_launcher plugin. homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_platform_interface # NOTE: We strongly prefer non-breaking changes, even at the expense of a # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes -version: 1.0.8 +version: 2.0.0-nullsafety dependencies: flutter: sdk: flutter - meta: ^1.0.5 - plugin_platform_interface: ^1.0.1 + # TODO (mvanbeusekom): use pub.dev once 1.10.0-nullsafety released. + plugin_platform_interface: + git: + url: https://github.com/flutter/plugins.git + ref: nnbd + path: packages/plugin_platform_interface dev_dependencies: flutter_test: sdk: flutter mockito: ^4.1.1 - pedantic: ^1.8.0 + pedantic: ^1.10.0-nullsafety.1 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.10.0-56.0.dev <3.0.0' flutter: ">=1.9.1+hotfix.4 <2.0.0" diff --git a/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart b/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart index 628ab48498ec..8c1b935daa2c 100644 --- a/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart +++ b/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// TODO(mvanbeusekom): Remove once Mockito is migrated to null safety. +// @dart = 2.9 import 'package:mockito/mockito.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; From b351ed16d62c67ca1f65d871a601eeeb9958980a Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Mon, 12 Oct 2020 21:47:00 +0200 Subject: [PATCH 2/6] Remove unused import of meta package --- .../lib/method_channel_url_launcher.dart | 1 - .../lib/url_launcher_platform_interface.dart | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart b/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart index 7eb6b7ad23e3..092cf1531d6f 100644 --- a/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart +++ b/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'package:flutter/services.dart'; -import 'package:meta/meta.dart' show required; import 'url_launcher_platform_interface.dart'; diff --git a/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart b/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart index 3c8d7dce642e..cd2fa56fc199 100644 --- a/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart +++ b/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart @@ -4,7 +4,6 @@ import 'dart:async'; -import 'package:meta/meta.dart' show required; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'method_channel_url_launcher.dart'; From 18a8d57f908dd5ce7720a2b0186264d13292cd8a Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Tue, 13 Oct 2020 10:22:18 +0200 Subject: [PATCH 3/6] canLaunch and launch should return false if value is null --- .../lib/method_channel_url_launcher.dart | 8 ++--- .../lib/url_launcher_platform_interface.dart | 4 +-- .../method_channel_url_launcher_test.dart | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart b/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart index 092cf1531d6f..1d66a4cc633c 100644 --- a/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart +++ b/packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart @@ -13,11 +13,11 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher'); /// An implementation of [UrlLauncherPlatform] that uses method channels. class MethodChannelUrlLauncher extends UrlLauncherPlatform { @override - Future canLaunch(String url) { + Future canLaunch(String url) { return _channel.invokeMethod( 'canLaunch', {'url': url}, - ); + ).then((value) => value ?? false); } @override @@ -26,7 +26,7 @@ class MethodChannelUrlLauncher extends UrlLauncherPlatform { } @override - Future launch( + Future launch( String url, { required bool useSafariVC, required bool useWebView, @@ -47,6 +47,6 @@ class MethodChannelUrlLauncher extends UrlLauncherPlatform { 'universalLinksOnly': universalLinksOnly, 'headers': headers, }, - ); + ).then((value) => value ?? false); } } diff --git a/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart b/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart index cd2fa56fc199..fae84c96f3ce 100644 --- a/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart +++ b/packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart @@ -38,7 +38,7 @@ abstract class UrlLauncherPlatform extends PlatformInterface { } /// Returns `true` if this platform is able to launch [url]. - Future canLaunch(String url) { + Future canLaunch(String url) { throw UnimplementedError('canLaunch() has not been implemented.'); } @@ -46,7 +46,7 @@ abstract class UrlLauncherPlatform extends PlatformInterface { /// /// For documentation on the other arguments, see the `launch` documentation /// in `package:url_launcher/url_launcher.dart`. - Future launch( + Future launch( String url, { required bool useSafariVC, required bool useWebView, diff --git a/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart b/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart index 8c1b935daa2c..eabb378eb446 100644 --- a/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart +++ b/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart @@ -43,6 +43,18 @@ void main() { final List log = []; channel.setMockMethodCallHandler((MethodCall methodCall) async { log.add(methodCall); + + if (methodCall.method == 'canLaunch') { + if (methodCall.arguments['url'] == 'http://example.com/null') { + return null; + } + } + + if (methodCall.method == 'launch') { + if (methodCall.arguments['url'] == 'http://example.com/null') { + return null; + } + } }); final MethodChannelUrlLauncher launcher = MethodChannelUrlLauncher(); @@ -63,6 +75,12 @@ void main() { ); }); + test('canLaunch should return false if platform returns null', () async { + final canLaunch = await launcher.canLaunch('http://example.com/null'); + + expect(canLaunch, false); + }); + test('launch', () async { await launcher.launch( 'http://example.com/', @@ -271,6 +289,20 @@ void main() { ); }); + test('launch should return false if platform returns null', () async { + final launched = await launcher.launch( + 'http://example.com/', + useSafariVC: true, + useWebView: false, + enableJavaScript: false, + enableDomStorage: false, + universalLinksOnly: false, + headers: const {}, + ); + + expect(launched, false); + }); + test('closeWebView default behavior', () async { await launcher.closeWebView(); expect( From 6e84ec9911bd04ddd7063fda2c3cd1060449f6cf Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Wed, 14 Oct 2020 09:07:14 +0200 Subject: [PATCH 4/6] Processed feedback on PR 3142 --- .../url_launcher_platform_interface/pubspec.yaml | 5 +---- .../test/method_channel_url_launcher_test.dart | 16 ++++------------ script/incremental_build.sh | 1 + 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml b/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml index 81aec6ac08e0..e01abe7e1dce 100644 --- a/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml +++ b/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml @@ -10,10 +10,7 @@ dependencies: sdk: flutter # TODO (mvanbeusekom): use pub.dev once 1.10.0-nullsafety released. plugin_platform_interface: - git: - url: https://github.com/flutter/plugins.git - ref: nnbd - path: packages/plugin_platform_interface + path: ../../plugin_platform_interface dev_dependencies: flutter_test: diff --git a/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart b/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart index eabb378eb446..7923e9fdaaf8 100644 --- a/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart +++ b/packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart @@ -44,17 +44,9 @@ void main() { channel.setMockMethodCallHandler((MethodCall methodCall) async { log.add(methodCall); - if (methodCall.method == 'canLaunch') { - if (methodCall.arguments['url'] == 'http://example.com/null') { - return null; - } - } - - if (methodCall.method == 'launch') { - if (methodCall.arguments['url'] == 'http://example.com/null') { - return null; - } - } + // Return null explicitly instead of relying on the implicit null + // returned by the method channel if no return statement is specified. + return null; }); final MethodChannelUrlLauncher launcher = MethodChannelUrlLauncher(); @@ -76,7 +68,7 @@ void main() { }); test('canLaunch should return false if platform returns null', () async { - final canLaunch = await launcher.canLaunch('http://example.com/null'); + final canLaunch = await launcher.canLaunch('http://example.com/'); expect(canLaunch, false); }); diff --git a/script/incremental_build.sh b/script/incremental_build.sh index c65d1d11eadc..c435674b7109 100755 --- a/script/incremental_build.sh +++ b/script/incremental_build.sh @@ -25,6 +25,7 @@ CUSTOM_ANALYSIS_PLUGINS=( "plugin_platform_interface" "video_player/video_player_web" "google_maps_flutter/google_maps_flutter_web" + "url_launcher/url_launcher_platform_interface" ) # Comma-separated string of the list above readonly CUSTOM_FLAG=$(IFS=, ; echo "${CUSTOM_ANALYSIS_PLUGINS[*]}") From 4555b5541213dc65d4604011f1ecbce53b4edc46 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Thu, 15 Oct 2020 10:21:54 +0200 Subject: [PATCH 5/6] Add missing analysis options --- .../url_launcher_platform_interface/analysis_options.yaml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 packages/url_launcher/url_launcher_platform_interface/analysis_options.yaml diff --git a/packages/url_launcher/url_launcher_platform_interface/analysis_options.yaml b/packages/url_launcher/url_launcher_platform_interface/analysis_options.yaml new file mode 100644 index 000000000000..32ff54f2748c --- /dev/null +++ b/packages/url_launcher/url_launcher_platform_interface/analysis_options.yaml @@ -0,0 +1,4 @@ +include: ../../../analysis_options.yaml +analyzer: + enable-experiment: + - non-nullable \ No newline at end of file From bce061246ceb92f8fe4fe4d178ff987a76213892 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Thu, 15 Oct 2020 17:55:06 +0200 Subject: [PATCH 6/6] Depend on plugin_platform_interface from GIT --- .../url_launcher_platform_interface/pubspec.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml b/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml index e01abe7e1dce..823456854872 100644 --- a/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml +++ b/packages/url_launcher/url_launcher_platform_interface/pubspec.yaml @@ -10,7 +10,10 @@ dependencies: sdk: flutter # TODO (mvanbeusekom): use pub.dev once 1.10.0-nullsafety released. plugin_platform_interface: - path: ../../plugin_platform_interface + git: + url: https://github.com/flutter/plugins.git + ref: nnbd + path: packages/plugin_platform_interface dev_dependencies: flutter_test: