及时从Firestore检索数据 [英] Retrieving Data From Firestore in time

查看:52
本文介绍了及时从Firestore检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上四处寻找解决问题的方法,但找不到任何解决方案.我在Firestore中使用Flutter来制作应用程序.我要这样做,以便用户在顶部登录应用程序时显示其名称(简单右侧).我可以从Firestore获取数据:

I have looked all over the internet for how to fix this problem and I can't find a solution anywhere. I am using flutter with firestore to make an app. I want to make it so when the user logins into the app at the top shows their name (simple right). I can get data from firestore:


Future<void> getName() async {
  print("Attemping to get name!");
  final firestoreInstance = await FirebaseFirestore.instance;
  FirebaseAuth auth = FirebaseAuth.instance;
  String uid = auth.currentUser.uid.toString();
  await firestoreInstance.collection("Users").doc(uid).get().then((value) {
    print("Name: " + value.data()["firstName"]);
    info.firstName=((value.data()["firstName"]));
  })

class info {
  static String firstName;
}


,但为时已晚,因此该应用仅在屏幕上显示"Welcome back"您可以在此处看到,而不是欢迎回来,康纳"如此处所示

but it comes in too late so the app just says "Welcome back" As you can see here and not "Welcome back, Connor" As seen here

当我在控制台中查看时,该函数确实运行,但程序未等待它,并继续导致null.

When I look in the console the function does run but the program doesn't wait for it and continues resulting in a null.

谢谢

根据请求的用户界面代码进行

EDIT as requested UI code:


class Home extends StatelessWidget {
  final DatabaseReference = FirebaseDatabase.instance;
  final FirebaseAuth auth = FirebaseAuth.instance;

  static String firstName;
  static String lastName;
  static String email;
  static String companyName;
  static bool isNewAccount = false;
  static String name = "Welcome back";

  Home();

  @override
  Widget build(BuildContext context) {
    final User user = auth.currentUser;
    final uid = user.uid;
    final ref = DatabaseReference.reference();

    if (isNewAccount == true) {
      userSetup(firstName, lastName, email, companyName);
      isNewAccount = false;
    }

    getName(); //does get the name from the database but name will error out as it doesn't get the name fast enough (or that's what I think)
    name= "Welcome back, "+info.firstName;

    return WillPopScope(
      onWillPop: () async => false,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          backgroundColor: Colors.cyan,
          body: SingleChildScrollView(
            child: Container(
              margin:
                  EdgeInsets.only(top: MediaQuery.of(context).size.height / 16),
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.only(bottom: 10),
                    child: Text(
                      name,
                      textAlign: TextAlign.center,
                      style: new TextStyle(
                        color: Colors.white,
                        fontSize: MediaQuery.of(context).size.width / 16,
                      ),
                    ),
                  ),
                  Container(
                    width: 260,
                    height: 125,
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0),
                      ),
                      child: Text(
                        "CHECK-IN",
                        style: new TextStyle(
                          color: Colors.white,
                          fontSize: 40.0,
                        ),
                      ),
                      color: Colors.grey[850],
                      onPressed: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (context) => Checkin()));
                      },
                    ),
                  ),
                  Container(
                    width: 260,
                    height: 140,
                    padding: EdgeInsets.only(top: 20),
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0),
                      ),
                      child: Text(
                        "CHECK-OUT",
                        style: new TextStyle(
                          color: Colors.white,
                          fontSize: 38.0,
                        ),
                      ),
                      color: Colors.grey[850],
                      onPressed: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => Checkout()));
                      },
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 55),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          padding: EdgeInsets.only(right: 20),
                          child: Text(
                            "Sign out: ",
                            style: new TextStyle(
                              fontSize: 15.0,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        Container(
                          child: RaisedButton(
                            child: Text(
                              "SIGN OUT",
                              style: new TextStyle(
                                fontSize: 14.0,
                                color: Colors.white,
                              ),
                            ),
                            color: Colors.grey[700],
                            onPressed: () {
                              context.read<AuthenticationService>().signOut();
                              //return MyApp();
                            },
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          padding: EdgeInsets.only(right: 20),
                          child: Text(
                            "View Stats: ",
                            style: new TextStyle(
                              fontSize: 15.0,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        Container(
                          child: RaisedButton(
                            child: Text(
                              "STATS",
                              style: new TextStyle(
                                fontSize: 14.0,
                                color: Colors.white,
                              ),
                            ),
                            color: Colors.grey[700],
                            onPressed: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => Stats()));
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          padding: EdgeInsets.only(right: 20),
                          child: Text(
                            "View Profile: ",
                            style: new TextStyle(
                              fontSize: 15.0,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        Container(
                          child: RaisedButton(
                            child: Text(
                              "PROFILE",
                              style: new TextStyle(
                                fontSize: 14.0,
                                color: Colors.white,
                              ),
                            ),
                            color: Colors.grey[700],
                            onPressed: () {
                              /*Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => Stats()));*/
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: 240,
                    height: 55,
                    margin: EdgeInsets.only(
                        top: MediaQuery.of(context).size.height / 12),
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0),
                      ),
                      child: Text(
                        "EMERGENCY REPORT",
                        style: new TextStyle(
                          color: Colors.white,
                          fontSize: 20.0,
                        ),
                      ),
                      color: Colors.grey[700],
                      onPressed: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (context) => EmergencyReport()));
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  static getAccountDetails(
      String firstName1, String lastName1, String email1, String companyName1) {
    firstName = firstName1;
    lastName = lastName1;
    email = email1;
    companyName = companyName1;
    isNewAccount = true;
  }

  static getFirstName(String nameFirst) {
    name = "Welcome back, "+nameFirst;
  }
}

推荐答案

FutureBuilder<DocumentSnapshot>(
          future: firestoreInstance.collection("Users").doc(uid).get(),
          builder: (_,snap){
          return snap.hasData ? Text(snap.data.data()["firstName"]):CircularProgressIndicator();
        },)

这篇关于及时从Firestore检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆