Future Builder列表视图的Flutter Firebase/Firestore文档参考

e0uiprwp  于 2023-03-13  发布在  Flutter
关注(0)|答案(1)|浏览(124)

我刚开始使用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())
                            );
                          },
                        );
                      }
                    });
            }}),
      ),
    );
  }
}

在我的数据文档中,出于外观原因,我在Map中设置了顺序字段和标题字段。顺序是我希望数据按照Firestore默认字母顺序排列的顺序。标题布尔是这样的,列表视图将被视为列表中标题的数据居中,并使其不可点击。
我确实得到了一个版本,在那里我使每个列表项在集合中成为自己的文档,但试图在单个文档中看到这种限制。

bnl4lu3b

bnl4lu3b1#

有排序功能内置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');

相关问题