'Flutter Firebase/Firestore Document Reference to Future Builder List View
Just started using Firestore and Flutter and trying to get my document items from Firestore into a ListView and running into errors. Here is my data structure:
and here is my code for the page so far.
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())
);
},
);
}
});
}}),
),
);
}
}
In my data document I have an order field and title field in the map for appearance reasons. Order is the order I want the data to be in as Firestore defaults alphabetical. Title bool is so the List View Centers data that is deemed a title in the list and make it so it is not tappable.
I did get a version working where I made each list item its own document in a collection but trying to see the limits of this in a single document.
Solution 1:[1]
there is sort functionality built-in with firestore there is method orderBy(field)
that you can use to sort data.
for more details see https://firebase.google.com/docs/firestore/query-data/order-limit-data
you need to edit this line
DocumentReference docRef = firestore.collection('Restaurant').document('Test');
to
DocumentReference docRef = firestore.collection('Restaurant').orderBy('order').document('Test');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tristian Nugent |