【发布时间】: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