'Stream user specific data from firestore using provider flutter

I have an app that the user has to log in to to get to the homepage. On the homepage is a map that should display markers based on a certain condition from the user. My Firestore model is simple Users > uid > userData. The examples I have come across using a provider and streaming user data use a simple model that says does not change the id of the fetched data.

The main app of my app looks like

MultiProvider(
        providers: [
          Provider<AuthenticationService>(
            create: (_) => AuthenticationService(FirebaseAuth.instance),
          ),
          Provider<DatabaseService>(
            create: (_) => DatabaseService(),
          ),
          StreamProvider(
            initialData: null,
            create: (context) =>
                context.read<AuthenticationService>().authStateChanges,
          ),
          StreamProvider(
            initialData: null,
            create: (context) =>
                context.read<AuthenticationService>().userChanges,
          ),
          /// this below provider works perfectly and fetches all parks
          /// since its just one collection of documents
          StreamProvider(
            initialData: null,
            create: (context) => context.read<DatabaseService>().getParks(),
          ),
           /// what I need is to stream user data based on the currently signed-in user
           /// so if userid 2 is signed in i'll stream user data of user 2 across the app
            StreamProvider(
            initialData: null,
            create: (context) => context.read<DatabaseService>().getCurrentUserData(),
          ),
          
        ],
        child: MaterialApp(), );

So what I have as the database class is:

class DatabaseService {
  final FirebaseFirestore db = FirebaseFirestore.instance;
  FirebaseAuth _firebaseAuth;

  Stream<List<Park>> getParks() {
    return db.collection('Parks').snapshots().map((snapShot) => snapShot.docs
        .map((document) => Park.fromJson(document.data()))
        .toList());
  }

  Stream<DocumentSnapshot> getCurrentUserData() {
     /// The problem comes when supplying the below uid of the user
     /// how can i stream the current user id of the user to here
     // and also prevent errors say when the app is in the signin page
      /// since then no user is present so we dont need to stream any user data

    return db.collection('Users').doc("uid").snapshots();
  }
}

How can I solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source