Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 68a2dea

Browse files
committed
canLaunch and launch should return false if value is null
1 parent ef71134 commit 68a2dea

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');
1313
/// An implementation of [UrlLauncherPlatform] that uses method channels.
1414
class MethodChannelUrlLauncher extends UrlLauncherPlatform {
1515
@override
16-
Future<bool?> canLaunch(String url) {
16+
Future<bool> canLaunch(String url) {
1717
return _channel.invokeMethod<bool>(
1818
'canLaunch',
1919
<String, Object>{'url': url},
20-
);
20+
).then((value) => value ?? false);
2121
}
2222

2323
@override
@@ -26,7 +26,7 @@ class MethodChannelUrlLauncher extends UrlLauncherPlatform {
2626
}
2727

2828
@override
29-
Future<bool?> launch(
29+
Future<bool> launch(
3030
String url, {
3131
required bool useSafariVC,
3232
required bool useWebView,
@@ -47,6 +47,6 @@ class MethodChannelUrlLauncher extends UrlLauncherPlatform {
4747
'universalLinksOnly': universalLinksOnly,
4848
'headers': headers,
4949
},
50-
);
50+
).then((value) => value ?? false);
5151
}
5252
}

packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ abstract class UrlLauncherPlatform extends PlatformInterface {
3838
}
3939

4040
/// Returns `true` if this platform is able to launch [url].
41-
Future<bool?> canLaunch(String url) {
41+
Future<bool> canLaunch(String url) {
4242
throw UnimplementedError('canLaunch() has not been implemented.');
4343
}
4444

4545
/// Returns `true` if the given [url] was successfully launched.
4646
///
4747
/// For documentation on the other arguments, see the `launch` documentation
4848
/// in `package:url_launcher/url_launcher.dart`.
49-
Future<bool?> launch(
49+
Future<bool> launch(
5050
String url, {
5151
required bool useSafariVC,
5252
required bool useWebView,

packages/url_launcher/url_launcher_platform_interface/test/method_channel_url_launcher_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ void main() {
4343
final List<MethodCall> log = <MethodCall>[];
4444
channel.setMockMethodCallHandler((MethodCall methodCall) async {
4545
log.add(methodCall);
46+
47+
if (methodCall.method == 'canLaunch') {
48+
if (methodCall.arguments['url'] == 'http://example.com/null') {
49+
return null;
50+
}
51+
}
52+
53+
if (methodCall.method == 'launch') {
54+
if (methodCall.arguments['url'] == 'http://example.com/null') {
55+
return null;
56+
}
57+
}
4658
});
4759

4860
final MethodChannelUrlLauncher launcher = MethodChannelUrlLauncher();
@@ -63,6 +75,12 @@ void main() {
6375
);
6476
});
6577

78+
test('canLaunch should return false if platform returns null', () async {
79+
final canLaunch = await launcher.canLaunch('http://example.com/null');
80+
81+
expect(canLaunch, false);
82+
});
83+
6684
test('launch', () async {
6785
await launcher.launch(
6886
'http://example.com/',
@@ -271,6 +289,20 @@ void main() {
271289
);
272290
});
273291

292+
test('launch should return false if platform returns null', () async {
293+
final launched = await launcher.launch(
294+
'http://example.com/',
295+
useSafariVC: true,
296+
useWebView: false,
297+
enableJavaScript: false,
298+
enableDomStorage: false,
299+
universalLinksOnly: false,
300+
headers: const <String, String>{},
301+
);
302+
303+
expect(launched, false);
304+
});
305+
274306
test('closeWebView default behavior', () async {
275307
await launcher.closeWebView();
276308
expect(

0 commit comments

Comments
 (0)