如何将Firestore数据获取到Flutter中的列表?

时间:2020-04-25 19:07:39

标签: flutter google-cloud-firestore

我在FireStore应用中正在检索的flutter中有一些值。

collecoll名称为News,并且只有一个字段为headline

我已经实现了将这个值转换为ListView的部分。但是我想要的是我想要使headline的值扑朔迷离List

这是我当前的设置,用于查看ListView

中的数据
return new Scaffold(
      appBar: AppBar(
        leading: new Icon(Icons.live_tv),
        title: const Text('SnapNews'),
        backgroundColor: Colors.red,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.help),
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => Help()));
            },
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          _show_search_bar(context), //calling the function
          Expanded(
              child: StreamBuilder(
            stream: FireStoreService().getNews(),
            builder:
                (BuildContext context, AsyncSnapshot<List<News>> snapshot) {
              if (snapshot.hasError || !snapshot.hasData) {
                return CircularProgressIndicator();
              } else {
                return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    News news = snapshot.data[index];
                    return ListTile(
                      title: Text(news.headline),
                    );
                  },
                );
              }
            },
          ))
        ],
      ),
      .
      .
      .
      .
    );

我还有另一个类似的功能:

Widget _show_search_bar(BuildContext context) {
  List list = []; //I want to get the values of headline here from the firestore
  return GFSearchBar(
    searchList: list,
    searchQueryBuilder: (query, list) {
      return list
          .where((item) => item.toLowerCase().contains(query.toLowerCase()))
          .toList();
    },
    overlaySearchListItemBuilder: (item) {
      return Container(
        padding: const EdgeInsets.all(8),
        child: Text(
          item,
          style: const TextStyle(fontSize: 18),
        ),
      );
    },
    onItemSelected: (item) {
      Navigator.push(context, MaterialPageRoute(builder: (_) => CameraScreen()));
    },
  );
}

有人可以帮助我吗?

0 个答案:

没有答案