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

Commit a26f14f

Browse files
author
Emmanuel Garcia
authored
[url_launcher] Migrates Link to null safety (#3196)
1 parent 318729c commit a26f14f

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

packages/url_launcher/url_launcher/lib/src/link.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Link extends StatelessWidget implements LinkInfo {
4040
final LinkWidgetBuilder builder;
4141

4242
/// The destination that this link leads to.
43-
final Uri uri;
43+
final Uri? uri;
4444

4545
/// The target indicating where to open the link.
4646
final LinkTarget target;
@@ -51,12 +51,11 @@ class Link extends StatelessWidget implements LinkInfo {
5151
/// Creates a widget that renders a real link on the web, and uses WebViews in
5252
/// native platforms to open links.
5353
Link({
54-
Key key,
55-
@required this.uri,
56-
LinkTarget target,
57-
@required this.builder,
58-
}) : target = target ?? LinkTarget.defaultTarget,
59-
super(key: key);
54+
Key? key,
55+
required this.uri,
56+
this.target = LinkTarget.defaultTarget,
57+
required this.builder,
58+
}) : super(key: key);
6059

6160
LinkDelegate get _effectiveDelegate {
6261
return UrlLauncherPlatform.instance.linkDelegate ??
@@ -90,16 +89,17 @@ class DefaultLinkDelegate extends StatelessWidget {
9089
bool get _useWebView {
9190
if (link.target == LinkTarget.self) return true;
9291
if (link.target == LinkTarget.blank) return false;
93-
return null;
92+
return false;
9493
}
9594

9695
Future<void> _followLink(BuildContext context) async {
97-
if (!link.uri.hasScheme) {
96+
if (!link.uri!.hasScheme) {
9897
// A uri that doesn't have a scheme is an internal route name. In this
9998
// case, we push it via Flutter's navigation system instead of letting the
10099
// browser handle it.
101100
final String routeName = link.uri.toString();
102-
return pushRouteNameToFramework(context, routeName);
101+
await pushRouteNameToFramework(context, routeName);
102+
return;
103103
}
104104

105105
// At this point, we know that the link is external. So we use the `launch`
@@ -119,7 +119,6 @@ class DefaultLinkDelegate extends StatelessWidget {
119119
context: ErrorDescription('during launching a link'),
120120
));
121121
}
122-
return Future<void>.value(null);
123122
}
124123

125124
@override

packages/url_launcher/url_launcher/test/link_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.8
5+
// TODO(egarciad): Remove once Mockito has been migrated to null safety.
6+
// @dart = 2.9
67

78
import 'dart:ui';
89
import 'package:flutter/material.dart';

packages/url_launcher/url_launcher_platform_interface/lib/link.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef FollowLink = Future<void> Function();
1616
/// the widget tree under it.
1717
typedef LinkWidgetBuilder = Widget Function(
1818
BuildContext context,
19-
FollowLink followLink,
19+
FollowLink? followLink,
2020
);
2121

2222
/// Signature for a delegate function to build the [Link] widget.
@@ -31,7 +31,7 @@ final MethodCodec _codec = const JSONMethodCodec();
3131
class LinkTarget {
3232
/// Const private constructor with a [debugLabel] to allow the creation of
3333
/// multiple distinct const instances.
34-
const LinkTarget._({this.debugLabel});
34+
const LinkTarget._({required this.debugLabel});
3535

3636
/// Used to distinguish multiple const instances of [LinkTarget].
3737
final String debugLabel;
@@ -64,7 +64,7 @@ abstract class LinkInfo {
6464
LinkWidgetBuilder get builder;
6565

6666
/// The destination that this link leads to.
67-
Uri get uri;
67+
Uri? get uri;
6868

6969
/// The target indicating where to open the link.
7070
LinkTarget get target;
@@ -80,10 +80,14 @@ Future<ByteData> pushRouteNameToFramework(
8080
String routeName, {
8181
@visibleForTesting bool debugForceRouter = false,
8282
}) {
83+
final PlatformMessageCallback? onPlatformMessage = window.onPlatformMessage;
84+
if (onPlatformMessage == null) {
85+
return Future<ByteData>.value(null);
86+
}
8387
final Completer<ByteData> completer = Completer<ByteData>();
8488
if (debugForceRouter || _hasRouter(context)) {
8589
SystemNavigator.routeInformationUpdated(location: routeName);
86-
window.onPlatformMessage(
90+
onPlatformMessage(
8791
'flutter/navigation',
8892
_codec.encodeMethodCall(
8993
MethodCall('pushRouteInformation', <dynamic, dynamic>{
@@ -94,7 +98,7 @@ Future<ByteData> pushRouteNameToFramework(
9498
completer.complete,
9599
);
96100
} else {
97-
window.onPlatformMessage(
101+
onPlatformMessage(
98102
'flutter/navigation',
99103
_codec.encodeMethodCall(MethodCall('pushRoute', routeName)),
100104
completer.complete,

packages/url_launcher/url_launcher_platform_interface/lib/method_channel_url_launcher.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');
1414
/// An implementation of [UrlLauncherPlatform] that uses method channels.
1515
class MethodChannelUrlLauncher extends UrlLauncherPlatform {
1616
@override
17-
final LinkDelegate linkDelegate = null;
17+
final LinkDelegate? linkDelegate = null;
1818

1919
@override
2020
Future<bool> canLaunch(String url) {

packages/url_launcher/url_launcher_platform_interface/lib/url_launcher_platform_interface.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class UrlLauncherPlatform extends PlatformInterface {
3939
}
4040

4141
/// The delegate used by the Link widget to build itself.
42-
LinkDelegate get linkDelegate;
42+
LinkDelegate? get linkDelegate;
4343

4444
/// Returns `true` if this platform is able to launch [url].
4545
Future<bool> canLaunch(String url) {

packages/url_launcher/url_launcher_platform_interface/test/link_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
// TODO(egarciad): Remove once Mockito has been migrated to null safety.
6+
// @dart = 2.9
7+
58
import 'dart:ui';
69

710
import 'package:mockito/mockito.dart';

0 commit comments

Comments
 (0)