Visitar URL original
Avoid calling setState on a disposed state (#79) · flutter/packages@82cb8ee · GitHub
Skip to content

Commit 82cb8ee

Browse files
authored
Avoid calling setState on a disposed state (#79)
1 parent 5799cff commit 82cb8ee

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎packages/vector_graphics/lib/vector_graphics.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class _VectorGraphicWidgetState extends State<VectorGraphic> {
166166

167167
void _loadAssetBytes() {
168168
widget.loader.loadBytes(context).then((ByteData data) {
169+
if (!mounted) {
170+
return;
171+
}
169172
final PictureInfo pictureInfo = decodeVectorGraphics(
170173
data,
171174
locale: Localizations.maybeLocaleOf(context),

‎packages/vector_graphics/test/vector_graphics_test.dart‎

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

5+
import 'dart:async';
56
import 'dart:typed_data';
67

78
import 'package:flutter/material.dart';
@@ -385,6 +386,22 @@ void main() {
385386

386387
expect(find.byKey(const ValueKey<int>(23)), findsOneWidget);
387388
});
389+
390+
testWidgets('Does not call setState after unmounting',
391+
(WidgetTester tester) async {
392+
final Completer<ByteData> completer = Completer<ByteData>();
393+
394+
await tester.pumpWidget(
395+
Directionality(
396+
textDirection: TextDirection.ltr,
397+
child: VectorGraphic(
398+
loader: DelayedBytesLoader(completer.future),
399+
),
400+
),
401+
);
402+
await tester.pumpWidget(const Placeholder());
403+
completer.complete(ByteData(0));
404+
});
388405
}
389406

390407
class TestAssetBundle extends Fake implements AssetBundle {
@@ -399,6 +416,25 @@ class TestAssetBundle extends Fake implements AssetBundle {
399416
}
400417
}
401418

419+
class DelayedBytesLoader extends BytesLoader {
420+
const DelayedBytesLoader(this.data);
421+
422+
final Future<ByteData> data;
423+
424+
@override
425+
Future<ByteData> loadBytes(BuildContext context) async {
426+
return await data;
427+
}
428+
429+
@override
430+
int get hashCode => data.hashCode;
431+
432+
@override
433+
bool operator ==(Object other) {
434+
return other is DelayedBytesLoader && other.data == data;
435+
}
436+
}
437+
402438
class TestBytesLoader extends BytesLoader {
403439
const TestBytesLoader(this.data);
404440

0 commit comments

Comments
 (0)