Flutter listen to one document change using provider

Viewed 307

I have seen examples of listening to document changes in streambuilder, but is it possible to use it in providers? I want to listen to changes in the document in userinfo collection.

my firebase config

Here is my code:

in databaseservice.dart

      Stream <DocumentSnapshot> get info{
        return userinfo.doc(uid).snapshots();
      }

In main

    return MultiProvider(
      providers: [
        StreamProvider<DocumentSnapshot>.value(
          value: DatabaseService().info
        ), // other providers

In wrapper where I need to see the change:

    final info = Provider.of<DocumentSnapshot>(context).data();

However, I'll first get error:

    The method 'data' was called on null.
    Receiver: null
    Tried calling: data()

And later, the info variable is giving me null instead of a map.

I want to let users input their name and age after their first signup, but not when they sign in. So my idea is that when users sign up, there will be a new document in the collection, "userinfo", which sets their age and name as null at first.

Then the wrapper checks if the name is null. If null, it will turn to the gather information page. If it has a value, it will turn to the home page.

Could anyone tell me where I am doing wrong with my document snapshot thing, or have a better idea to implement this?

0 Answers
Related