Skip to content

Commit

Permalink
Migrate from AsyncValue.when to Dart 3's switch-case
Browse files Browse the repository at this point in the history
  • Loading branch information
natsuk4ze committed Jan 25, 2024
1 parent b7f030c commit 6fb7159
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
10 changes: 5 additions & 5 deletions lib/features/package_details/package_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class PackageDetailsPage extends ConsumerWidget {
appBar: TitleAppBar(title: l10n.packageDetailsPage.title),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: package.when(
data: (package) => PackegeDetailsItem(package),
error: (e, _) => Text(e.toString()),
loading: () => const Center(child: CircularProgressIndicator()),
),
child: switch (package) {
AsyncData(:final value) => PackegeDetailsItem(value),
AsyncError(:final error) => Text(error.toString()),
_ => const Center(child: CircularProgressIndicator()),
},
),
);
}
Expand Down
53 changes: 26 additions & 27 deletions lib/features/packages/packages_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'packages_page.g.dart';

@visibleForTesting
const initialSearchText = 'color';
const initialSearchText = 'vue';

@riverpod
class Sort extends _$Sort {
Expand Down Expand Up @@ -111,32 +111,31 @@ class _PackageItems extends ConsumerWidget {
final sort = ref.watch(sortProvider);
final l10n = ref.watch(l10nProvider);

return packages.when(
data: (packages) {
final sortedPackages = sort == null
? List.of(packages)
: packages.sortedByCompare(
(package) => sort.getValue(package.score),
(a, b) => b.compareTo(a));
return switch (packages) {
AsyncData(:final value) => Builder(builder: (context) {
final sortedPackages = sort == null
? List.of(value)
: value.sortedByCompare((package) => sort.getValue(package.score),
(a, b) => b.compareTo(a));

return sortedPackages.isEmpty
? SingleChildScrollView(
child: EmptyImage(text: l10n.packagesPage.packageNotFound),
)
: RefreshIndicator(
onRefresh: () async => ref.refresh(packagesProvider(
search: searchText,
debounce: false,
).future),
child: ListView.separated(
separatorBuilder: (_, __) => const Divider(),
itemCount: sortedPackages.length,
itemBuilder: (_, int i) => PackageItem(sortedPackages[i]),
),
);
},
error: (e, _) => Text(e.toString()),
loading: () => const Center(child: CircularProgressIndicator()),
);
return sortedPackages.isEmpty
? SingleChildScrollView(
child: EmptyImage(text: l10n.packagesPage.packageNotFound),
)
: RefreshIndicator(
onRefresh: () async => ref.refresh(packagesProvider(
search: searchText,
debounce: false,
).future),
child: ListView.separated(
separatorBuilder: (_, __) => const Divider(),
itemCount: sortedPackages.length,
itemBuilder: (_, int i) => PackageItem(sortedPackages[i]),
),
);
}),
AsyncError(:final error) => Text(error.toString()),
_ => const Center(child: CircularProgressIndicator()),
};
}
}

0 comments on commit 6fb7159

Please sign in to comment.