在另一个类中显示firestore数据库信息;颤振,Android Studio

时间:2021-03-28 16:06:39

标签: firebase flutter dart google-cloud-firestore

有人能告诉我如何显示 firebase 中用户的数据吗?例如想要显示名称。我确实有来自 firebasetutorial(一次性阅读)的 GetUserInfo 类,可以在我认为的 documentID 和我想显示信息的 einstellungen 类中阅读它。但是如何将信息从 documentID 获取到另一个类中的 text

 class GetUserInfo extends StatelessWidget {
      final String documentID;
      GetUserInfo(this.documentID);
    
      @override
      Widget build(BuildContext context) {
        CollectionReference users = FirebaseFirestore.instance.collection('users');
        return FutureBuilder<DocumentSnapshot>(
          future: users.doc(documentID).get(),
          builder:
              (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.hasError) {
              return Text("Something went wrong");
            }
            if (snapshot.connectionState == ConnectionState.done) {
              Map<String, dynamic> data = snapshot.data.data();
              return Text("Name: ${data['name:']}");
            }
            return Text("loading");
          },
        );
      }
    }
    
    class einstellungen extends StatelessWidget {
      final user = FirebaseAuth.instance.currentUser;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.red,
            title: Text("Einstellungen"),
          ),
          backgroundColor: Colors.orange,
          body: Column(
            children: [
              Container(
                  margin: const EdgeInsets.only(
                      left: 8.0, right: 8.0, top: 10.0, bottom: 5.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Colors.white.withOpacity(0.2),
                  ),
                  child: Column(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Padding(
                          padding: const EdgeInsets.only(
                              bottom: 10.0, left: 10.0, right: 10.0),
                          child: Text(''),         // Name from the user here; maybe the mail in a next child
                        ),
                      ),
                    ],
                  )),
              if (user.uid != null)
                Text(
                  'Cloud User-ID: ' + user.uid,  // this is just from firebaseauth
                  style: TextStyle(color: Colors.black),
                ),
            ],
          ),
        );
      }
    }

Firebase data

非常感谢!!!

0 个答案:

没有答案