Skip to content

Commit ac9e622

Browse files
LeGoffMaelAlexV525
andauthored
🐛 Fix disposed provider throwing error when fetching assets (#493)
Co-authored-by: Alex Li <[email protected]>
1 parent 0215ca7 commit ac9e622

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/src/provider/asset_picker_provider.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
6060
_paths.clear();
6161
_currentPath = null;
6262
_currentAssets.clear();
63+
_mounted = false;
6364
super.dispose();
6465
}
6566

67+
@override
68+
void notifyListeners() {
69+
if (_mounted) {
70+
super.notifyListeners();
71+
}
72+
}
73+
74+
/// Whether the provider is mounted. Set to `false` if disposed.
75+
bool get mounted => _mounted;
76+
bool _mounted = true;
77+
6678
/// Get paths.
6779
/// 获取所有的资源路径
6880
Future<void> getPaths();
@@ -490,6 +502,10 @@ class DefaultAssetPickerProvider
490502
final PathWrapper<AssetPathEntity> wrapper = _currentPath!;
491503
final int assetCount =
492504
wrapper.assetCount ?? await wrapper.path.assetCountAsync;
505+
// If the picker was disposed (#492), stop fetching the assets
506+
if (!mounted) {
507+
return;
508+
}
493509
totalAssetsCount = assetCount;
494510
isAssetsEmpty = assetCount == 0;
495511
if (wrapper.assetCount == null) {

0 commit comments

Comments
 (0)