Skip to content

Commit dca3675

Browse files
ditmanyutaaraki-toydium
authored andcommitted
[url_launcher] Fixes call to setState after dispose. (flutter#5963)
1 parent 56b5ff8 commit dca3675

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

packages/url_launcher/url_launcher_web/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.0.12
2+
3+
* Fixes call to `setState` after dispose on the `Link` widget.
4+
[Issue](https://github.com/flutter/flutter/issues/102741).
5+
* Removes unused `BuildContext` from the `LinkViewController`.
6+
17
## 2.0.11
28

39
* Minor fixes for new analysis options.

packages/url_launcher/url_launcher_web/example/integration_test/link_widget_test.dart

+30
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ void main() {
123123
final html.Element anchor = _findSingleAnchor();
124124
expect(anchor.hasAttribute('href'), false);
125125
});
126+
127+
testWidgets('can be created and disposed', (WidgetTester tester) async {
128+
final Uri uri = Uri.parse('http://foobar');
129+
const int itemCount = 500;
130+
await tester.pumpWidget(
131+
Directionality(
132+
textDirection: TextDirection.ltr,
133+
child: MediaQuery(
134+
data: const MediaQueryData(),
135+
child: ListView.builder(
136+
itemCount: itemCount,
137+
itemBuilder: (_, int index) => WebLinkDelegate(TestLinkInfo(
138+
uri: uri,
139+
target: LinkTarget.defaultTarget,
140+
builder: (BuildContext context, FollowLink? followLink) =>
141+
Text('#$index', textAlign: TextAlign.center),
142+
)),
143+
),
144+
),
145+
),
146+
);
147+
148+
await tester.pumpAndSettle();
149+
150+
await tester.scrollUntilVisible(
151+
find.text('#${itemCount - 1}'),
152+
2500,
153+
maxScrolls: 1000,
154+
);
155+
});
126156
});
127157
}
128158

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class WebLinkDelegateState extends State<WebLinkDelegate> {
7676
child: PlatformViewLink(
7777
viewType: linkViewType,
7878
onCreatePlatformView: (PlatformViewCreationParams params) {
79-
_controller = LinkViewController.fromParams(params, context);
79+
_controller = LinkViewController.fromParams(params);
8080
return _controller
8181
..setUri(widget.link.uri)
8282
..setTarget(widget.link.target);
@@ -100,7 +100,7 @@ class WebLinkDelegateState extends State<WebLinkDelegate> {
100100
/// Controls link views.
101101
class LinkViewController extends PlatformViewController {
102102
/// Creates a [LinkViewController] instance with the unique [viewId].
103-
LinkViewController(this.viewId, this.context) {
103+
LinkViewController(this.viewId) {
104104
if (_instances.isEmpty) {
105105
// This is the first controller being created, attach the global click
106106
// listener.
@@ -113,12 +113,17 @@ class LinkViewController extends PlatformViewController {
113113
/// platform view [params].
114114
factory LinkViewController.fromParams(
115115
PlatformViewCreationParams params,
116-
BuildContext context,
117116
) {
118117
final int viewId = params.id;
119-
final LinkViewController controller = LinkViewController(viewId, context);
118+
final LinkViewController controller = LinkViewController(viewId);
120119
controller._initialize().then((_) {
121-
params.onPlatformViewCreated(viewId);
120+
/// Because _initialize is async, it can happen that [LinkViewController.dispose]
121+
/// may get called before this `then` callback.
122+
/// Check that the `controller` that was created by this factory is not
123+
/// disposed before calling `onPlatformViewCreated`.
124+
if (_instances[viewId] == controller) {
125+
params.onPlatformViewCreated(viewId);
126+
}
122127
});
123128
return controller;
124129
}
@@ -159,9 +164,6 @@ class LinkViewController extends PlatformViewController {
159164
@override
160165
final int viewId;
161166

162-
/// The context of the [Link] widget that created this controller.
163-
final BuildContext context;
164-
165167
late html.Element _element;
166168

167169
bool get _isInitialized => _element != null;
@@ -208,7 +210,7 @@ class LinkViewController extends PlatformViewController {
208210
// browser handle it.
209211
event.preventDefault();
210212
final String routeName = _uri.toString();
211-
pushRouteNameToFramework(context, routeName);
213+
pushRouteNameToFramework(null, routeName);
212214
}
213215

214216
Uri? _uri;

packages/url_launcher/url_launcher_web/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: url_launcher_web
22
description: Web platform implementation of url_launcher
33
repository: https://github.com/flutter/plugins/tree/main/packages/url_launcher/url_launcher_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
5-
version: 2.0.11
5+
version: 2.0.12
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)