gpt4 book ai didi

firebase - Flutter - 排序 Cloud Firestore

转载 作者:IT王子 更新时间:2023-10-29 07:18:57 26 4
gpt4 key购买 nike

我正在尝试对传入的 ListView 进行排序。由于此 ListView 是图像,因此我向 firestore 对象添加了一个数字,以便可以升序排序。

我似乎无法对项目进行排序,但我确信这是我构建应用程序的方式。

我试图将 .orderBy() 添加到集合中,但是它说 Query 不是一种 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"]),
),
),
),

);
}

))
],
),
);
}
}

我真的很想按照数据库中的预定数字对这些图像进行排序。

最佳答案

我认为这只是一个简单的疏忽引入的类型错误。 (请下次为您的应用附加错误案例,我在这里猜测。)

你有:

CollectionReference collectionReference = Firestore.instance.collection("Events");

使用 orderBy,您应该:

Query collectionReference = Firestore.instance.collection("Events").orderBy('field');

orderBy 应该返回一个 Query,您不能再将它存储为 CollectionReference

关于firebase - Flutter - 排序 Cloud Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com