Skip to content

Commit

Permalink
#5 ui: use asyncValue widget
Browse files Browse the repository at this point in the history
  • Loading branch information
surajadkhari committed Aug 20, 2022
1 parent a3a9856 commit 172e79d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
58 changes: 39 additions & 19 deletions lib/features/presentation/widgets/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:fetch_api_with_proper_architecure/features/data/models/post_response_model.dart';
import 'package:fetch_api_with_proper_architecure/features/presentation/controllers/post_controller.dart';
import 'package:fetch_api_with_proper_architecure/utils/asyncvalue_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

Expand All @@ -7,25 +9,43 @@ class HomePage extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final data = ref.watch(postControllerProvider);
final result = ref.watch(postControllerProvider);
return Scaffold(
body:
data.when(
data: (data) {
return ListView(
children: [
...data.map((e) => ListTile(
contentPadding: const EdgeInsets.all(20),
title: Text(
"${e.firstName} ${e.lastName}",
),
trailing: Image.network(e.avatar),
))
],
);
},
error: (error, stackTrace) => Text(error.toString()),
loading: () => const CircularProgressIndicator()),
);
body: AsyncValueWidget<List<PostResponseModel>>(
value: result,
data: (data) {
return ListView(
children: [
...data.map((e) => ListTile(
contentPadding: const EdgeInsets.all(20),
title: Text(
"${e.firstName} ${e.lastName}",
),
trailing: Image.network(e.avatar),
))
],
);
},
)
// data.when(
// data: (data) {
// return ListView(
// children: [
// ...data.map((e) => ListTile(
// contentPadding: const EdgeInsets.all(20),
// title: Text(
// "${e.firstName} ${e.lastName}",
// ),
// trailing: Image.network(e.avatar),
// ))
// ],
// );
// },
// error: (error, stackTrace) => Text(
// error.toString(),
// ),
// loading: () => const CircularProgressIndicator(),
// ),
);
}
}
6 changes: 3 additions & 3 deletions lib/utils/asyncvalue_widget.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class AsyncValueWidget extends StatelessWidget {
class AsyncValueWidget<T> extends StatelessWidget {
const AsyncValueWidget({Key? key, required this.value, required this.data})
: super(key: key);
final AsyncValue value;
final Widget Function(dynamic) data;
final AsyncValue<T> value;
final Widget Function(T) data;
@override
Widget build(BuildContext context) {
return Consumer(
Expand Down

0 comments on commit 172e79d

Please sign in to comment.