如何使用StreamBuilder从一页导航到另一页

时间:2020-04-29 18:16:20

标签: firebase flutter google-cloud-firestore flutter-layout flutter-test

我想检查用户的电话号码是否存在于Firestre中?如果它存在于Firestore中,则它将导航到MyHomePage()。它不是导航到MyHomePage

我的代码是

StreamBuilder<QuerySnapshot>(
                    stream: _firestore.collection('users').snapshots(),
                     builder: (context, snapshot){
                            if(!snapshot.hasData){
                              return Text('Please Enter data');
                            }
                            final ph=snapshot.data.documents;
                            List<String> store=[];
                            for(var phonenum in ph){
                                  final catA=phonenum.data['Phone'];
                                  store=[catA];
                                  for(int i=0;i>=store.length;i++){
                                    if(_phone==store[i]){
                                      Navigator.pushNamed(context, MyHomePage.id);
                                    }
                                    else{
                                      Text('Phone is not registered yet');
                                    }

                                  }

                                };?

1 个答案:

答案 0 :(得分:0)

您可以使用DocumentSnapshot而不是使用Querysnapshot。它非常适合您的情况。

代码概述:

Future checkIfAvailableOrNot() async{
DocumentSnapshot doc = await db.collection('users').document(_phone).get();
if(doc.exists)
{
   //navigate to other page
}
else{
   print('no user found');
}
}