Sami Karaeen
Sami Karaeen

Reputation: 91

Flutter Firebase Reqd one document

I have a landing page and a collection in firestore that i save some information about a user in (e.g if he passed the On Boarding Screen or not). The next time i open the app, i want to check if the user passed the onboarding. If they did: I redirect to another screen; if not, then redirect to onboarding.

All the examples are reading a collection and get all documents, but i need to read only one document which i know the documentid .

i have tried to get the data like this, but did not work.

class LandingPage extends StatelessWidget {
  // LandingPage({@required this.auth});
  // final AuthBase auth;
     @override
      Widget build(BuildContext context) {
        final auth = Provider.of<AuthBase>(context, listen: false);
        return StreamBuilder<User>(
          stream: auth.onAuthStateChanged,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              User user = snapshot.data;
                  if (user == null) {
                return ChooseLanguage(
                    //     auth: auth,
                    );
              }

    // he i need to do a call to a ducment in the firestore
    //if the retuen is true then i go to page 1 else go to page 2

            } else {
              return Scaffold(
                body: Center(child: CircularProgressIndicator()),
              );
            }
          },
        );
      }
    }

do i have to do -iama beginner in flutter

Upvotes: 1

Views: 69

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

To get one document do the following:

getData()async{
await Firestore.instance.collection("users").document(id).get().then((value){
      print(value.data['name']);
    });
} 

You need to use the correct collection name, value.data will contain the data of that document which you can access using the [] operator.


According to your edit, you are checking if checkonboarding() is null, but since it returns a future you need to use await:

    void sendData()async{
        bool value = await checkonBoarding(user);
      if (value != null) {
            return Provider<Database>(
              create: (_) => new FireStoreDatabase(uid: user.uid),
              child: Dashboard(usr: user, auth: auth),
            );
          } else {
            return Provider<Database>(
              create: (_) => new FireStoreDatabase(uid: user.uid),
              child: Onboarding(usr: user, auth: auth),
            );
          }
       }

Upvotes: 2

Related Questions