【问题标题】:from firestore map to StreamBuilder => ListView.Builder从 firestore 地图到 StreamBuilder => ListView.Builder
【发布时间】:2020-08-08 12:21:20
【问题描述】:

我想在特定文档中显示歌曲列表,并且只显示 KEY(name) 而不是值。每首歌曲都应加载到列表中,但所有歌曲都加载到一个小部件中。 它会从 Firestore 中的每个文档(歌手)加载“歌曲列表”,并显示 KEY 和 Value。

class SongsList extends StatefulWidget {
  @override
  _SongsListState createState() => _SongsListState();
}

class _SongsListState extends State<SongsList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: Firestore.instance.collection('singers').snapshots(),
        builder: (
          context,
          snapshot,
        ) {
          //**if statement**
          return Container(
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/back.png'), fit: BoxFit.contain)),
            child: ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) => SingleChildScrollView(
                      child: Padding(
                        padding: const EdgeInsets.only(
                            left: 10, right: 10, top: 10, bottom: 0),
                        child: Container(
                          height: 50,
                          width: 300,
                          decoration: BoxDecoration(
                              color: Colors.white,
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.white.withOpacity(0.5),
                                  spreadRadius: 1.5,
                                  blurRadius: 1.5,
                                  //offset: Offset(0, 1), // changes position of shadow
                                ),
                              ],
                              borderRadius: BorderRadius.circular(5),
                              border: Border.all(
                                  color: Colors.red[200],
                                  width: 0.5,
                                  style: BorderStyle.solid)),
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  snapshot.data.documents[index]['songs list']
                                      .toString(),
                                  style: TextStyle(
                                      fontSize: 10, color: Colors.red[500]),
                                ),
                              ]),
                        ),
                      ),
                    )),
          );
        },
      ),
    );
  }
}

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您可以执行以下操作:

    snapshot.data.documents[index]['songs list']
    

    这应该返回一个map,因此将它分配给一个变量:

    var result = snapshot.data.documents[index]['songs list'];
    

    然后迭代并将key 分配给Text 小部件:

    children: <Widget>[
            for(var res in result.entries) 
              Text(
                res.key,
                  style: TextStyle(
                      fontSize: 10, color: Colors.red[500]),
                 ),
             ]
    

    完整代码:

    class SongsList extends StatefulWidget {
      @override
      _SongsListState createState() => _SongsListState();
    }
    
    class _SongsListState extends State<SongsList> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: StreamBuilder(
            stream: Firestore.instance.collection('singers').snapshots(),
            builder: (
              context,
              snapshot,
            ) {
              //**if statement**
              return Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage('assets/back.png'), fit: BoxFit.contain)),
                child: ListView.builder(
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index) {
                      var result = snapshot.data.documents[index]['songs list'];
                      return SingleChildScrollView(
                        child: Padding(
                          padding: const EdgeInsets.only(
                              left: 10, right: 10, top: 10, bottom: 0),
                          child: Container(
                            height: 50,
                            width: 300,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                boxShadow: [
                                  BoxShadow(
                                    color: Colors.white.withOpacity(0.5),
                                    spreadRadius: 1.5,
                                    blurRadius: 1.5,
                                    //offset: Offset(0, 1), // changes position of shadow
                                  ),
                                ],
                                borderRadius: BorderRadius.circular(5),
                                border: Border.all(
                                    color: Colors.red[200],
                                    width: 0.5,
                                    style: BorderStyle.solid)),
                            child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  for (var res in result.entries)
                                    Text(
                                      res.key,
                                      style: TextStyle(
                                          fontSize: 10, color: Colors.red[500]),
                                    ),
                                ]),
                          ),
                        ),
                      );
                    }),
              );
            },
          ),
        );
      }
    }
    

    还要确保将 pubspec.yaml 文件中的 dart 版本更改为以下内容:

    environment:
      sdk: ">=2.7.0 <3.0.0"
    

    然后执行:

     flutter pub get
     flutter clean
    

    然后重启 vscode 或 android studio。

    【讨论】:

    • 感谢@peter。嘿,彼得,我是新来的颤振。你能在我的代码中更正上面问题的代码吗,因为我尝试了多次,但没有正确审查。提前致谢
    • 感谢@peter。它就像它是什么。它显示所有文档(歌手)中的“歌曲列表”,并在单个文本小部件(不是列表)中显示每个列表
    猜你喜欢
    • 2020-11-29
    • 1970-01-01
    • 2021-06-05
    • 2020-01-09
    • 1970-01-01
    • 2019-10-01
    • 2020-10-01
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多