user13891541
user13891541

Reputation:

I want to check in firestore document whether a particular user exits or not

enter image description here

When my app runs i am calling firestore collection with the exact document name. My requirement is such that if the documentId already exist then it will return a Blank container and if it doesn't not exist then it will return a Raised Button where details can be added . Currentlty i am using a stream builder but it is not working.

Widget build(BuildContext context) {

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(title: Text('KartOfill',),backgroundColor: Colors.red,),
          body: StreamBuilder<Object>(
            stream: Firestore.instance.collection('users').document('9954603381').snapshots(),
            builder: (context, snapshot) {

            

              if(snapshot.hasData){
              return Container();
              }

              return Container(
                child: RaisedButton(
                  onPressed: () async{

                    var firebaseUser = await FirebaseAuth.instance.currentUser();
                    firestore.collection("users").document('9954603381').setData(
                        {
                          "name" : "john",
                          "age" : 50,
                          "email" : "[email protected]",
                          "address" : {
                            "street" : "street 24",
                            "city" : "new york"
                          }
                        }).then((_){
                      print("success!");
                    });





                  },
                ),


              );
            }
          ),
        ),
      ),
    );
  }

Upvotes: 0

Views: 173

Answers (1)

lenz
lenz

Reputation: 2425

Here's how you could use a FutureBuilder instead

Firestore firestore = Firestore.instance;

Widget build(BuildContext context) {
  return MaterialApp(
    debugShowCheckedModeBanner: false,
    home: SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            'KartOfill',
          ),
          backgroundColor: Colors.red,
        ),
        body: MyWidget(),
      ),
    ),
  );
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: Future.wait(
          [
            _getDoc(),
            _getFirebaseUser()
          ]
        ),
        builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
          DocumentSnapshot doc = snapshot.data[0];
          FirebaseUser firebaseUser = snapshot.data[1];
          if (doc.exists) {
            return Container();
          }

          return Container(
            child: RaisedButton(
              onPressed: () {
                // FirebaseUser firebaseUser; //defined above
                firestore.collection("users").document('9954603381').setData({
                  "name": "john",
                  "age": 50,
                  "email": "[email protected]",
                  "address": {"street": "street 24", "city": "new york"}
                }).then((_) {
                  print("success!");
                });
              },
            ),
          );
        });
  }
}

Future<DocumentSnapshot> _getDoc() => 
    firestore.collection('users').document('9954603381').get();

Future<FirebaseUser> _getFirebaseUser() => 
    FirebaseAuth.instance.currentUser();

Upvotes: 1

Related Questions