Project

General

Profile

Actions

Task #28

open

Abstract Flutter Services as Follows

Task #28: Abstract Flutter Services as Follows

Added by Fadi Hussein about 1 month ago. Updated about 1 month ago.

Status:
In Progress
Priority:
Normal
Assignee:
Milad Khnefes
Target version:
Sunnex - V1.1
Start date:
01/29/2026
Due date:
% Done:

90%

Estimated time:
17:00 h
Spent time:

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

repository_factory.dart (670 Bytes) repository_factory.dart Milad Khnefes, 01/29/2026 02:07 PM
authenticated_client.dart (4.1 KB) authenticated_client.dart Milad Khnefes, 01/29/2026 02:07 PM
client.dart (4.49 KB) client.dart Milad Khnefes, 01/29/2026 02:07 PM
auth_urls.dart (212 Bytes) auth_urls.dart Milad Khnefes, 01/29/2026 02:07 PM
profile_urls.dart (150 Bytes) profile_urls.dart Milad Khnefes, 01/29/2026 02:07 PM
auth_repository.dart (2.69 KB) auth_repository.dart Milad Khnefes, 02/01/2026 12:07 PM
base_repository.dart (32 Bytes) base_repository.dart Milad Khnefes, 02/01/2026 12:07 PM
profile_repository.dart (2.96 KB) profile_repository.dart Milad Khnefes, 02/01/2026 12:07 PM
repository_factory.dart (758 Bytes) repository_factory.dart Milad Khnefes, 02/01/2026 12:07 PM
authenticated_client.dart (2.31 KB) authenticated_client.dart Milad Khnefes, 02/01/2026 12:07 PM
client.dart (3.8 KB) client.dart Milad Khnefes, 02/01/2026 12:07 PM
base_api_client.dart (4.38 KB) base_api_client.dart Milad Khnefes, 02/01/2026 02:32 PM

Updated by Milad Khnefes about 1 month ago Actions #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 about 1 month ago Actions #3

  • Estimated time set to 17:00 h

Updated by Milad Khnefes about 1 month ago Actions #4

Implemented a modified version of Base Results Service

Updated by Milad Khnefes about 1 month ago Actions #5

  • % Done changed from 10 to 70

Finished service abstraction according to best OOP practices.
Moving on to Riverpod

Updated by Milad Khnefes about 1 month ago Actions #6

Started implementing notifiers, finished 2 so far, ConfigNotifiers and AdsNotifiers

Updated by Milad Khnefes about 1 month ago Actions #7

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 about 1 month ago Actions #8

  • % Done changed from 70 to 90

Finished all providers

Actions

Also available in: PDF Atom