【问题标题】:Flutter and Firebase: Show stored information from cloud_firestore in widgetFlutter 和 Firebase:在小部件中显示来自 cloud_firestore 的存储信息
【发布时间】:2021-06-19 14:55:46
【问题描述】:

我想显示我的 firebase cloudfirestore 中的数据,如图所示:

在设置页面中,我想显示登录用户的所有信息。 这是设置代码(德语中的“einstellungen”):

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: SingleChildScrollView(
        child: 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.all(10.0),
                        child: Text('Benutzer:',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.only(
                            bottom: 10.0, left: 10.0, right: 10.0),
                        child: Text(''),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Text('Spitzname:',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.only(
                            bottom: 10.0, left: 10.0, right: 10.0),
                        child: Text(''),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Text('E-Mail:',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.only(
                            bottom: 10.0, left: 10.0, right: 10.0),
                        child: Text(''), // here should print the email address from firestore
                      ),
                    ),
                    Text(''),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Text('Hinweis: ',
                            style: TextStyle(fontWeight: FontWeight.bold)),
                      ),
                    ),
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Text(
                            'Wenn Du dein Konto löschen oder dein Passwort zurücksetzen möchtest, schreibe bitte eine Email an medien@visuhome.de'),
                      ),
                    ),
                    Text(''),
                  ],
                )),
            Container(
              margin: const EdgeInsets.only(
                  left: 8.0, right: 8.0, top: 3.0, bottom: 3.0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                color: Colors.white.withOpacity(0.2),
              ),
              child: Row(
                children: [
                  ButtonNutzung(),
                  Text("Nutzungsbedingungen",
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: Divider(),
            ),
            SizedBox(height: 10),
            if (user.uid != null)
              Text(
                'Cloud User-ID: ' + user.uid,
                style: TextStyle(color: Colors.black),
              ),
            SizedBox(height: 10),
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: Divider(),
            ),
            GestureDetector(
              onTap: _launchURL2,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(left: 20.0, right: 20.0),
                    child: Text(
                      'created by visuhome',
                      style: TextStyle(
                          fontWeight: FontWeight.w300,
                          fontSize: 12,
                          color: Colors.black),
                    ),
                  ),
                  Image(
                    //AssetImage oder SizedBox mit child und width
                    image: AssetImage('assets/visuhome_logo_black_flutter.png'),
                    fit: BoxFit.fitHeight,
                    height: 30,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 20.0, right: 20.0),
                    child: Text(
                      'Version 1.0',
                      style: TextStyle(
                          fontWeight: FontWeight.w300,
                          fontSize: 12,
                          color: Colors.black),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

如您所见,我确实从FirebaseAuth.instance.currentUser 获取信息,因此显示了来自登录用户的userid。但我也想要firestore中用户数据的信息。

我猜有人可以帮忙解决这个基本的 firestore 技巧吗?我是新来的。尝试了很多,但找不到适合我的例子的解决方案。非常感谢

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您应该创建对 Firestore 的查询并获取用户详细信息。

    如果知道用户ID,可以调用doc函数创建查询。

     @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("Full Name: ${data['full_name']} ${data['last_name']}");
            }
    
            return Text("loading");
          },
        );
      }
    

    或者使用 where 表达式通过其属性(例如电子邮件)查找用户:

    FirebaseFirestore.instance
      .collection('users')
      .where('email', isEqualTo: "test@yahoo.de")
      .get()
    

    更多详情请查看 Firestore tutorial

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @Logemann 是的,你是对的。我会解决的。
    • 您好,谢谢!教程页面很棒。我使用您共享的代码(来自教程页面的代码)在“einstellungen”类上方创建了一个新类“GetUserInfo”。但是如何在我的类“einstellungen”中显示名称?
    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多