Skip to content

Commit ad063fb

Browse files
committed
♻️ Refactored switchPath's signature
1 parent b10c11d commit ad063fb

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ class FileAssetPickerProvider extends AssetPickerProvider<File, Directory> {
369369
}
370370

371371
@override
372-
Future<void> switchPath(Directory pathEntity) async {
372+
Future<void> switchPath([Directory? pathEntity]) async {
373+
if (pathEntity == null) {
374+
return;
375+
}
373376
isSwitchingPath = false;
374377
currentPathEntity = pathEntity;
375378
totalAssetsCount = 0;

guides/migration_guide.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ you can stop reading._
5757

5858
#### `AssetPickerProvider`
5959

60-
- The `switchPath` method is now `Future<void>`.
60+
- The `switchPath` method has a different signature:
61+
62+
Before:
63+
64+
```dart
65+
void switchPath(P pathEntity);
66+
```
67+
68+
After:
69+
70+
```dart
71+
Future<void> switchPath([P? pathEntity]);
72+
```
6173

6274
## 5.0.0
6375

lib/src/provider/asset_picker_provider.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ abstract class AssetPickerProvider<A, P> extends ChangeNotifier {
232232

233233
/// Switch path entity.
234234
/// 切换路径
235-
Future<void> switchPath(P pathEntity);
235+
Future<void> switchPath([P? pathEntity]);
236236
}
237237

238238
class DefaultAssetPickerProvider
@@ -366,7 +366,27 @@ class DefaultAssetPickerProvider
366366
}
367367

368368
@override
369-
Future<void> switchPath(AssetPathEntity pathEntity) async {
369+
Future<void> switchPath([AssetPathEntity? pathEntity]) async {
370+
assert(() {
371+
if (_currentPathEntity == null && pathEntity == null) {
372+
throw FlutterError.fromParts(<DiagnosticsNode>[
373+
ErrorSummary('Empty $AssetPathEntity was switched.'),
374+
ErrorDescription(
375+
'Neither currentPathEntity nor pathEntity is non-null, which makes '
376+
'this method useless.',
377+
),
378+
ErrorHint(
379+
'You need to pass a non-null $AssetPathEntity or call this method '
380+
'when currentPathEntity is not null.',
381+
),
382+
]);
383+
}
384+
return true;
385+
}());
386+
if (_currentPathEntity == null && pathEntity == null) {
387+
return;
388+
}
389+
pathEntity ??= _currentPathEntity!;
370390
_isSwitchingPath = false;
371391
_currentPathEntity = pathEntity;
372392
_totalAssetsCount = pathEntity.assetCount;

lib/src/widget/asset_picker.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,9 @@ class AssetPickerState<A, P> extends State<AssetPicker<A, P>>
267267
if (!widget.builder.isPermissionLimited) {
268268
return;
269269
}
270-
final P? pathEntity = widget.builder.provider.currentPathEntity;
271-
if (pathEntity != null) {
272-
if (pathEntity is AssetPathEntity) {
273-
await pathEntity.refreshPathProperties();
274-
}
275-
await widget.builder.provider.getAssetPathList();
276-
await widget.builder.provider.switchPath(pathEntity);
270+
await widget.builder.provider.getAssetPathList();
271+
if (widget.builder.provider.currentPathEntity != null) {
272+
await widget.builder.provider.switchPath();
277273
}
278274
}
279275

0 commit comments

Comments
 (0)