How to design multiple stream provider to retrieve data from on a same table in firestore using flutter?

Viewed 86

I am very new to flutter and dart programming and struggling with a scenario and not able to decide the best approach to solve it

Requirement : 1- screen 1 - Display list of all the users in the system on a screen 2- screen 2- Display filtered list of users ( for instance, filters based on the role ) 3- screen 3 - Display filtered list of users ( filter may be based on location)

I have implemented the first point using the below approach and want to understand how to implement the other two points. I have already created a stream provider for the first one. How to stream filtered data?

Current implementation:

Main.dart:

StreamProvider<List<UserModel>>(
      create: (BuildContext context) => userProvider.getUserList(),
      initialData: const [],
    ),
    StreamProvider<UserModel>(
      create: (BuildContext context) =>
          userProvider.getUserProfileDetails(),
      initialData: UserModel(),
    ),

Userprovider.dart

class UserProvider extends ChangeNotifier {

final FirebaseFirestore _firestore = FirebaseFirestore.instance; User? user = FirebaseAuth.instance.currentUser;

UserModel loggedUser = UserModel();

UserModel get loggedInUser => UserModel();

Stream<List> getUserList() { return _firestore .collection("users") .snapshots() .map((snapshot) => snapshot.docs.map((docs) => UserModel.fromJson(docs.data())).toList()); }

Stream getUserProfileDetails() {

return _firestore 
    .collection("users")
    .doc(user!.uid)
    .snapshots()
    .map((sanp) => UserModel.fromJson(sanp.data() as Map<String, dynamic>));

}

**How do I use the below stream that has filtered data in user screen 2? **

Stream<List> getRolespecificList(String role) {

return FirebaseFirestore.instance
    .collection("users")
    .where('role', isEqualTo: role)
    .snapshots().map((snapshot) =>
    snapshot.docs.map((docs) => UserModel.fromJson(docs.data())).toList());

}

}

UserScreen1.dart

final userList = Provider.of<List>(context)

UserScreen2.dart

final filteredUserList = ??????

How do I refer to the filtered stream (getRolespecificList(String role)) created in the provider.dart

0 Answers
Related