FutureBuilder小部件未返回我想要的页面- Flutter Firebase

at0kjp5o  于 2023-01-18  发布在  Flutter
关注(0)|答案(1)|浏览(93)

当我尝试使用FutureBuilder小部件等待来自Firebase数据库的布尔值时,它没有返回我想要的值。
返回的LoginPage()运行良好。
正在执行print1和print2。6

Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Auth().authStateChanges,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return FutureBuilder(
              future: _getCurrentUserData(),
              builder: ((context, AsyncSnapshot<bool> hasAccountSetup) {
                if (hasAccountSetup.data == true) {
                  print("print1");
                  return MyHomePage();
                } else {
                  print("print2");
                  return AccountSetup();
                }
              }));
        } else {
          return LoginPage();
        }
      },
    );
  }

  Future<bool> _getCurrentUserData() async {
    final DocumentSnapshot userDoc = await FirebaseFirestore.instance
        .collection('Users')
        .doc(Auth().currentUser!.uid)
        .get();

    return userDoc.get('HasSetupAccount');
  }
qij5mzcb

qij5mzcb1#

如果HasSetupAccount是firestore数据库中的字段,请尝试以下操作:

Future<bool> _getCurrentUserData() async {
    final DocumentSnapshot userDoc = await FirebaseFirestore.instance
        .collection('Users')
        .doc(Auth().currentUser!.uid)
        .get();
        Map<String, dynamic> data = docSnapshot.data()!;
    //HasSetupAccount should be the name of the field you want to access
    return data['HasSetupAccount']; 
}

相关问题