首页 > 解决方案 > 如何从firebase颤振中检索当前用户字段

问题描述

我试图通过使用 FutureBuilder 从 firebase 获取当前用户的一些信息。但它告诉我错误说

 return new Scaffold(
      body: FutureBuilder(
        future: FirebaseFirestore.instance
            .collection('Users').where('email', isEqualTo: FirebaseAuth.instance.currentUser.email).get(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.data == null) {
            return Container(
              child: Center(
                child: CircularProgressIndicator(),
              ),
            );
          } else if (snapshot.connectionState == ConnectionState.done) {

            print(snapshot.data.docs.length);

            return Container(
              child: ListView(children: <Widget>[
                ListTile(
                  leading: Icon(Icons.person),
                  title: Text("Name: " +
                      snapshot.data.docs[0].get('Name') +
                      "\n" +
                      "Civil ID: " +
                      snapshot.data.docs[0].get('Civil ID')),
                  subtitle: Text(snapshot.data.docs[0].get('email')),
                ),

标签: flutterdartflutter-futurebuilder

解决方案


Try this

    FirebaseAuth auth = FirebaseAuth.instance;

 return new Scaffold(
      body: FutureBuilder(
        future: FirebaseFirestore.instance
            .collection('Users').where('email', isEqualTo: auth.currentUser.email).get(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {

          if (snapshot.data == null) {
            return Container(
              child: Center(
                child: CircularProgressIndicator(),
              ),
            );
          } else if (snapshot.connectionState == ConnectionState.done) {

            print(snapshot.data.docs.length);

            return Container(
              child: ListView(children: <Widget>[
                ListTile(
                  leading: Icon(Icons.person),
                  title: Text("Name: " +
                      snapshot.data.docs[0].get('Name') +
                      "\n" +
                      "Civil ID: " +
                      snapshot.data.docs[0].get('Civil ID')),
                  subtitle: Text(snapshot.data.docs[0].get('email')),
                ),




//If that don't work, you might have to upload a picture of how you structured your database

推荐阅读