【问题标题】:Flutter Firebase/Firestore Document Reference to Future Builder List ViewFlutter Firebase/Firestore 文档对 Future Builder 列表视图的参考
【发布时间】:2019-05-10 08:45:05
【问题描述】:

刚开始使用 Firestore 和 Flutter 并尝试将我的文档项从 Firestore 获取到 ListView 并遇到错误。这是我的数据结构:

到目前为止,这是我的页面代码。

import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_firestore_test/ui/restaurantDetail.dart';

class RestaurantList extends StatefulWidget {

  final String title;
  final String director;
  RestaurantList({Key key,this.title, this.director}) : super(key: key);



  @override
  _RestaurantListState createState() => _RestaurantListState();
}

class _RestaurantListState extends State<RestaurantList> {

  Future getList() async{
    var firestore = Firestore.instance;

    DocumentReference docRef = firestore.collection('Restaurant').document('Test');
    var data;
    docRef.get().then((datasnapshot){
      if(datasnapshot.exists){
        data = datasnapshot.data['Locations'];
      }
    });
    return data;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('${widget.title}'),
        centerTitle: true,
        backgroundColor: Colors.blueGrey,
      ),
      body: new Container(
        child: FutureBuilder(
            future: getList(),
            builder: (_, snapshot){
              if(snapshot.connectionState == ConnectionState.waiting){
                return CircularProgressIndicator();
//                return Center(
//                  child: Text("Loading..."),
//                );
              }else{
                return ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (_, index){
                      if(snapshot.data[index].data["title"]==true){
                        return ListTile(
                          title: new Center(child: Text(snapshot.data[index].data["name"],
                            style: TextStyle(fontSize: 25.0),),),
                        );
                      }else if(snapshot.data[index].data["title"]==false) {
                        return ListTile(
                          title: Text(snapshot.data[index].data["name"],
                          style: TextStyle(
                              fontWeight: FontWeight.w500,
                              color: Colors.black),
                          ),
                          subtitle: Text('Insert Type of food?',
                          style: TextStyle(
                            color: Colors.grey),
                          ),
                          onTap: (){
                            Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context)=> RestaurantDetail())
                            );
                          },
                        );
                      }
                    });
            }}),
      ),
    );
  }
}

在我的数据文档中,出于外观原因,我在地图中有一个订单字段和标题字段。 Order 是我希望数据按照 Firestore 默认字母顺序排列的顺序。 Title bool 是 List View Centers 数据被视为列表中的标题并使其不可点击。

我确实得到了一个版本,在该版本中,我在集合中将每个列表项都设为了自己的文档,但试图在单个文档中查看此限制。

【问题讨论】:

  • “遇到错误” 什么错误?请编辑您的问题以包含错误消息和堆栈跟踪。
  • 说明 Locations [] 为空

标签: firebase flutter google-cloud-firestore


【解决方案1】:

firestore 内置了排序功能,您可以使用方法orderBy(field) 对数据进行排序。 更多详情见https://firebase.google.com/docs/firestore/query-data/order-limit-data

你需要编辑这一行

DocumentReference docRef = firestore.collection('Restaurant').document('Test');

DocumentReference docRef = firestore.collection('Restaurant').orderBy('order').document('Test');

【讨论】:

  • 我认为这不会像我们在文档参考中那样起作用。它需要先获取文件,然后才能订购。对吗?
  • 所以您尝试解析的文档字段是“位置”,对吗?如果是这样,则没有 snapshot.data[index].data["title"]
  • 没错。本质上,我正在尝试在餐厅文档中的位置地图中构建所有项目的列表。我实验的最终目标是看看我是否可以仅使用一个文档来构建完整的餐厅列表。
猜你喜欢
  • 2019-06-14
  • 2019-06-14
  • 1970-01-01
  • 2021-03-25
  • 2018-06-16
  • 2021-11-19
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多