如何从firebase firestore数据库生成小部件列表?

问题描述 投票:0回答:1

我正在试图弄清楚如何遍历firestore中的文档并为每个文档创建一个文本小部件。

我已经弄明白了如何访问这些元素。我使用了debugPrint()并得到了我期待的结果,但我不能让它显示在我的其他小部件下面。我得到一个有大量错误的红色屏幕(在电话上).Below是我迄今为止尝试过的代码。


   QuerySnapshot querySnapshot = await 
Firestore.instance.collection("users").document(user.uid).collection("trails").getDocuments();
        var list = querySnapshot.documents;
list.forEach((doc) =>  debugPrint(doc.data["Trail Name"].toString()));//works
final topContentText = Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
      //these widgets are generating correctly  
      Text(
          "First Name: $firstName",
          style: TextStyle(color: Colors.white, ),
        ),
        Text(
          "Last Name: $lastName",
          textAlign: TextAlign.left,
          style: TextStyle(color: Colors.white,),
        ),
        Text(
          "Email: $email",
          style: TextStyle(color: Colors.white, ),
        ),
     //these ones are causing my errors.
    list.forEach((doc) =>  Text(
      "Trail Name: ${doc.data["Trail Name"].toString()}",
      style: TextStyle(color: Colors.white, ),
    ),)
      ],
    );

    final topContent = Stack(
      children: <Widget>[
        Container(
          height: MediaQuery.of(context).size.height * 0.40,
          padding: EdgeInsets.all(40.0),
          width: MediaQuery.of(context).size.width,
          decoration: BoxDecoration(color: Color.fromRGBO(58, 66, 86, .9)),
          child: Center(
            child: topContentText,
          ),
        ),
        Positioned(
          left: 8.0,
          top: 60.0,
          child: InkWell(
            onTap: () {
              Navigator.pop(context);
            },
            child: Icon(Icons.arrow_back, color: Colors.white),
          ),
        )
      ],
    );
    return Scaffold(
      body: Column(
        children: <Widget>[topContent, bottomContent],
      ),
    );

当我希望它显示其他信息下方的路径名称时,我的设备上的屏幕亮红色,创建子窗口小部件时出错。我只包含必要的代码,但如果需要可以包含更多(例如小部件的构建方法)。谢谢你的帮助。我是新来的人。

firebase flutter google-cloud-firestore
1个回答
1
投票

尝试使用map函数:

  List<Widget> _widgets = list.map((doc) =>  Text(
    "Trail Name: ${doc.data["Trail Name"].toString()}",
    style: TextStyle(color: Colors.white, ),
  ),).toList();

然后对于列的子项,只需将该列表添加到您指定的列表中,例如:

children: [ ... ] + _widgets;
© www.soinside.com 2019 - 2024. All rights reserved.