Flutter-对Cloud Firestore进行排序 [英] Flutter - Sorting Cloud Firestore
问题描述
我正在尝试对即将到来的列表视图进行排序.由于此列表视图是图像,因此我在firestore对象中添加了一个数字,以便可以升序排序.
I am trying to sort the listview that is coming in. Since this listview is images, I added a number to the firestore object so I can sort ascending.
我似乎无法对项目进行排序,而且我确定它是如何构建应用程序的.
I cannot seem to get the items to sort, and I am sure its how I built the app.
我试图将.orderBy()添加到集合中,但是它说查询不是CollectionReference的一种类型.
I tried to add the .orderBy() to the collection, however its says Query is not a type of CollectionReference.
这是我的页面
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
class Events extends StatefulWidget {
@override
_EventsState createState() => _EventsState();
}
class _EventsState extends State<Events> {
StreamSubscription<QuerySnapshot> subscription;
List<DocumentSnapshot> snapshot;
CollectionReference collectionReference =
Firestore.instance.collection("Events");
@override
void initState() {
subscription = collectionReference.snapshots().listen((datasnapshot) {
setState(() {
snapshot = datasnapshot.documents;
});
});
super.initState();
}
// passData(DocumentSnapshot snap) {
// Navigator.of(context).push(
// MaterialPageRoute(builder: (context) => EventPage(snapshot: snap,)));
// }
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: 200,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage("assets/images/events.jpg"),
),
),
),
Divider(
color: Colors.black,
),
Expanded(
child: ListView.separated(separatorBuilder: (context, index) => Divider(color: Colors.black12,
), itemCount: snapshot.length,
itemBuilder: (context, index){
return Card(
child: Container(
width: MediaQuery.of(context).size.width,
height: 210,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(snapshot[index].data["image"]),
),
),
),
);
}
))
],
),
);
}
}
我真的很想在数据库中按预定数量对这些图像进行排序.
I really would like to sort these images by predetermined numbers in teh database.
推荐答案
我认为这只是您通过简单的疏忽而引入的类型错误.(请下次在您的应用中添加错误情况,我想是在这里.)
I think it's just a type error you introduces by a simple oversight. (Please, next time append the error case for you app, I am guessing here.)
您有:
CollectionReference collectionReference = Firestore.instance.collection("Events");
使用 orderBy
,您应该具有:
Query collectionReference = Firestore.instance.collection("Events").orderBy('field');
orderBy
应该返回一个 Query
,您不能再将其存储为 CollectionReference
.
orderBy
should returns a Query
, you can no longer store it as a CollectionReference
.
这篇关于Flutter-对Cloud Firestore进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!