【问题标题】:how to use StreamBuilder for navigation from one page to another page如何使用 StreamBuilder 从一页导航到另一页
【发布时间】:2020-08-13 22:58:42
【问题描述】:

我想检查用户的电话号码是否存在于 firestre 中?如果它存在于 Firestore 中,那么它将导航到 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');
                                    }

                                  }

                                };?

【问题讨论】:

  • 您遇到什么错误?也请检查此post
  • 该页面未导航到我的主页。

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


【解决方案1】:

您可以使用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');
}
}

【讨论】:

    猜你喜欢
    • 2017-12-14
    • 1970-01-01
    • 2013-02-09
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多