Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix disposed provider throwing error when fetching assets #493

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/src/provider/asset_picker_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,25 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
/// 销毁时重置所有内容
@override
void dispose() {
_mounted = false;
LeGoffMael marked this conversation as resolved.
Show resolved Hide resolved
_isAssetsEmpty = false;
_paths.clear();
_currentPath = null;
_currentAssets.clear();
super.dispose();
}

@override
void notifyListeners() {
if (_mounted) {
super.notifyListeners();
}
}
Comment on lines +67 to +72
Copy link
Collaborator Author

@LeGoffMael LeGoffMael Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is actually optional since it is not needed to fix #492 issue.
However, notifyListeners() will throw an exception if the provider was disposed, so i thought it was still good to put a flag on it.


/// 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,8 @@ 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;
LeGoffMael marked this conversation as resolved.
Show resolved Hide resolved
totalAssetsCount = assetCount;
isAssetsEmpty = assetCount == 0;
if (wrapper.assetCount == null) {
Expand Down