Actions
Task #28
closed
Abstract Flutter Services as Follows
Task #28:
Abstract Flutter Services as Follows
Status:
Closed
Priority:
Normal
Assignee:
Milad Khnefes
Target version:
Sunnex - V1.1
Description
class ApiRepository {
// Logic for Users
Future<User> getUser() async {
await Future.delayed(const Duration(seconds: 1));
return User(name: "Gemini");
}
// Logic for Posts
Future<List<String>> getPosts() async {
await Future.delayed(const Duration(seconds: 1));
return ["Post 1", "Post 2"];
}
}
// Make the repository itself available via a provider
final apiRepositoryProvider = Provider((ref) => ApiRepository());
class UserNotifier extends AsyncNotifier<User> {
@override
FutureOr<User> build() {
// Access the repository logic
return ref.watch(apiRepositoryProvider).getUser();
}
// Action specific to users
Future<void> updateName(String newName) async {
state = const AsyncLoading();
// Logic to update user...
}
}
final userProvider = AsyncNotifierProvider<UserNotifier, User>(UserNotifier.new);
class PostsNotifier extends AsyncNotifier<List<String>> {
@override
FutureOr<List<String>> build() {
return ref.watch(apiRepositoryProvider).getPosts();
}
}
final postsProvider = AsyncNotifierProvider<PostsNotifier, List<String>>(PostsNotifier.new);
class DashboardScreen extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
// Watch multiple providers independently
final userAsync = ref.watch(userProvider);
final postsAsync = ref.watch(postsProvider);
return Scaffold(
body: Column(
children: [
// User Section
userAsync.when(
data: (user) => Text("Welcome, ${user.name}"),
loading: () => CircularProgressIndicator(),
error: (e, _) => Text("User Error"),
),
// Posts Section
Expanded(
child: postsAsync.when(
data: (posts) => ListView(children: posts.map(Text.new).toList()),
loading: () => Center(child: CircularProgressIndicator()),
error: (e, _) => Text("Posts Error"),
),
),
],
),
);
}
}
@riverpod
Future<User> fetchUser(Ref ref) async {
return ref.watch(apiRepositoryProvider).getUser();
}
Files
Updated by Milad Khnefes 6 months ago
- File repository_factory.dart repository_factory.dart added
- File authenticated_client.dart authenticated_client.dart added
- File client.dart client.dart added
- File auth_urls.dart auth_urls.dart added
- File profile_urls.dart profile_urls.dart added
- Status changed from New to In Progress
- Target version set to V1.1
I've finished the the Repositories and Notifiers of Auth and Profile.
I'm almost through with the ApiClient abstraction and Repositories encapsulation.
Updated by Milad Khnefes 5 months ago
- File auth_repository.dart auth_repository.dart added
- File base_repository.dart base_repository.dart added
- File profile_repository.dart profile_repository.dart added
- File repository_factory.dart repository_factory.dart added
- File authenticated_client.dart authenticated_client.dart added
- File client.dart client.dart added
- % Done changed from 0 to 10
Updated by Milad Khnefes 5 months ago
- Estimated time set to 17:00 h
Updated by Milad Khnefes 5 months ago
- File base_api_client.dart base_api_client.dart added
Implemented a modified version of Base Results Service
Updated by Milad Khnefes 5 months ago
- % Done changed from 10 to 70
Finished service abstraction according to best OOP practices.
Moving on to Riverpod
Updated by Milad Khnefes 5 months ago
Started implementing notifiers, finished 2 so far, ConfigNotifiers and AdsNotifiers
Updated by Milad Khnefes 5 months ago
Enhanced both of the previous notifiers, filled some gaps in the implementation, and tested everything out visually to ensure it works.
Updated by Milad Khnefes 5 months ago
- % Done changed from 70 to 90
Finished all providers
Updated by Fadi Hussein 3 months ago
- Status changed from In Progress to Closed
Actions