Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/url_launcher/url_launcher/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Link extends StatelessWidget implements LinkInfo {
final LinkWidgetBuilder builder;

/// The destination that this link leads to.
final Uri uri;
final Uri? uri;

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

LinkDelegate get _effectiveDelegate {
return UrlLauncherPlatform.instance.linkDelegate ??
Expand Down Expand Up @@ -90,16 +89,17 @@ class DefaultLinkDelegate extends StatelessWidget {
bool get _useWebView {
if (link.target == LinkTarget.self) return true;
if (link.target == LinkTarget.blank) return false;
return null;
return false;
}

Future<void> _followLink(BuildContext context) async {
if (!link.uri.hasScheme) {
if (!link.uri!.hasScheme) {
// A uri that doesn't have a scheme is an internal route name. In this
// case, we push it via Flutter's navigation system instead of letting the
// browser handle it.
final String routeName = link.uri.toString();
return pushRouteNameToFramework(context, routeName);
await pushRouteNameToFramework(context, routeName);
return;
Copy link
Member

Choose a reason for hiding this comment

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

Is this nnbd or just better practice? If pushRouteNameToFramework returns Future, why not return that from here? Is it because it's in another package and it's some Future<void>??

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering the same thing.

Copy link
Author

Choose a reason for hiding this comment

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

Otherwise, the analyzer complains:

A value of type 'ByteData' can't be returned from method '_followLink' because it has a return type of 'Future<void>'.dartreturn_of_invalid_type

Also, it doesn't seem like the intention is to return Future<ByteData>.

Copy link
Member

Choose a reason for hiding this comment

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

Ooooh good catch by the analyzer, I wonder how come that wasn't an error before nnbd!

Copy link
Contributor

Choose a reason for hiding this comment

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

Casting anything to void used to be fine. I'm not sure why it changed with nnbd.

}

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

@override
Expand Down
3 changes: 2 additions & 1 deletion packages/url_launcher/url_launcher/test/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

import 'dart:ui';
import 'package:flutter/material.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef FollowLink = Future<void> Function();
/// the widget tree under it.
typedef LinkWidgetBuilder = Widget Function(
BuildContext context,
FollowLink followLink,
FollowLink? followLink,
);

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

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

/// The destination that this link leads to.
Uri get uri;
Uri? get uri;

/// The target indicating where to open the link.
LinkTarget get target;
Expand All @@ -80,10 +80,14 @@ Future<ByteData> pushRouteNameToFramework(
String routeName, {
@visibleForTesting bool debugForceRouter = false,
}) {
final PlatformMessageCallback? onPlatformMessage = window.onPlatformMessage;
if (onPlatformMessage == null) {
return Future<ByteData>.value(null);
}
final Completer<ByteData> completer = Completer<ByteData>();
if (debugForceRouter || _hasRouter(context)) {
SystemNavigator.routeInformationUpdated(location: routeName);
window.onPlatformMessage(
onPlatformMessage(
'flutter/navigation',
_codec.encodeMethodCall(
MethodCall('pushRouteInformation', <dynamic, dynamic>{
Expand All @@ -94,7 +98,7 @@ Future<ByteData> pushRouteNameToFramework(
completer.complete,
);
} else {
window.onPlatformMessage(
onPlatformMessage(
'flutter/navigation',
_codec.encodeMethodCall(MethodCall('pushRoute', routeName)),
completer.complete,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
final LinkDelegate linkDelegate = null;
final LinkDelegate? linkDelegate = null;

@override
Future<bool> canLaunch(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class UrlLauncherPlatform extends PlatformInterface {
}

/// The delegate used by the Link widget to build itself.
LinkDelegate get linkDelegate;
LinkDelegate? get linkDelegate;

/// Returns `true` if this platform is able to launch [url].
Future<bool> canLaunch(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(egarciad): Remove once Mockito has been migrated to null safety.
// @dart = 2.9

import 'dart:ui';

import 'package:mockito/mockito.dart';
Expand Down