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

问题描述 投票:0回答:1

我想检查用户的电话号码是否存在于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');
                                    }

                                  }

                                };?
firebase flutter google-cloud-firestore flutter-layout flutter-test
1个回答
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');
}
}
© www.soinside.com 2019 - 2024. All rights reserved.