Skip to content

Commit

Permalink
[url_launcher] Fixes call to setState after dispose. (flutter#5963)
Browse files Browse the repository at this point in the history
  • Loading branch information
ditman authored and yutaaraki-toydium committed Aug 12, 2022
1 parent 56b5ff8 commit dca3675
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
6 changes: 6 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.12

* Fixes call to `setState` after dispose on the `Link` widget.
[Issue](https://github.com/flutter/flutter/issues/102741).
* Removes unused `BuildContext` from the `LinkViewController`.

## 2.0.11

* Minor fixes for new analysis options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ void main() {
final html.Element anchor = _findSingleAnchor();
expect(anchor.hasAttribute('href'), false);
});

testWidgets('can be created and disposed', (WidgetTester tester) async {
final Uri uri = Uri.parse('http://foobar');
const int itemCount = 500;
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(),
child: ListView.builder(
itemCount: itemCount,
itemBuilder: (_, int index) => WebLinkDelegate(TestLinkInfo(
uri: uri,
target: LinkTarget.defaultTarget,
builder: (BuildContext context, FollowLink? followLink) =>
Text('#$index', textAlign: TextAlign.center),
)),
),
),
),
);

await tester.pumpAndSettle();

await tester.scrollUntilVisible(
find.text('#${itemCount - 1}'),
2500,
maxScrolls: 1000,
);
});
});
}

Expand Down
20 changes: 11 additions & 9 deletions packages/url_launcher/url_launcher_web/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class WebLinkDelegateState extends State<WebLinkDelegate> {
child: PlatformViewLink(
viewType: linkViewType,
onCreatePlatformView: (PlatformViewCreationParams params) {
_controller = LinkViewController.fromParams(params, context);
_controller = LinkViewController.fromParams(params);
return _controller
..setUri(widget.link.uri)
..setTarget(widget.link.target);
Expand All @@ -100,7 +100,7 @@ class WebLinkDelegateState extends State<WebLinkDelegate> {
/// Controls link views.
class LinkViewController extends PlatformViewController {
/// Creates a [LinkViewController] instance with the unique [viewId].
LinkViewController(this.viewId, this.context) {
LinkViewController(this.viewId) {
if (_instances.isEmpty) {
// This is the first controller being created, attach the global click
// listener.
Expand All @@ -113,12 +113,17 @@ class LinkViewController extends PlatformViewController {
/// platform view [params].
factory LinkViewController.fromParams(
PlatformViewCreationParams params,
BuildContext context,
) {
final int viewId = params.id;
final LinkViewController controller = LinkViewController(viewId, context);
final LinkViewController controller = LinkViewController(viewId);
controller._initialize().then((_) {
params.onPlatformViewCreated(viewId);
/// Because _initialize is async, it can happen that [LinkViewController.dispose]
/// may get called before this `then` callback.
/// Check that the `controller` that was created by this factory is not
/// disposed before calling `onPlatformViewCreated`.
if (_instances[viewId] == controller) {
params.onPlatformViewCreated(viewId);
}
});
return controller;
}
Expand Down Expand Up @@ -159,9 +164,6 @@ class LinkViewController extends PlatformViewController {
@override
final int viewId;

/// The context of the [Link] widget that created this controller.
final BuildContext context;

late html.Element _element;

bool get _isInitialized => _element != null;
Expand Down Expand Up @@ -208,7 +210,7 @@ class LinkViewController extends PlatformViewController {
// browser handle it.
event.preventDefault();
final String routeName = _uri.toString();
pushRouteNameToFramework(context, routeName);
pushRouteNameToFramework(null, routeName);
}

Uri? _uri;
Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_web
description: Web platform implementation of url_launcher
repository: https://github.com/flutter/plugins/tree/main/packages/url_launcher/url_launcher_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 2.0.11
version: 2.0.12

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit dca3675

Please sign in to comment.