Skip to content

Commit

Permalink
🐛 Fix disposed provider throwing error when fetching assets (flutterc…
Browse files Browse the repository at this point in the history
…andies#493)

Co-authored-by: Alex Li <[email protected]>
  • Loading branch information
LeGoffMael and AlexV525 authored Sep 27, 2023
1 parent 0215ca7 commit ac9e622
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/src/provider/asset_picker_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,21 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
_paths.clear();
_currentPath = null;
_currentAssets.clear();
_mounted = false;
super.dispose();
}

@override
void notifyListeners() {
if (_mounted) {
super.notifyListeners();
}
}

/// Whether the provider is mounted. Set to `false` if disposed.
bool get mounted => _mounted;
bool _mounted = true;

/// Get paths.
/// 获取所有的资源路径
Future<void> getPaths();
Expand Down Expand Up @@ -490,6 +502,10 @@ class DefaultAssetPickerProvider
final PathWrapper<AssetPathEntity> wrapper = _currentPath!;
final int assetCount =
wrapper.assetCount ?? await wrapper.path.assetCountAsync;
// If the picker was disposed (#492), stop fetching the assets
if (!mounted) {
return;
}
totalAssetsCount = assetCount;
isAssetsEmpty = assetCount == 0;
if (wrapper.assetCount == null) {
Expand Down

0 comments on commit ac9e622

Please sign in to comment.