Flutter - 排序 Cloud Firestore

Bnd*_*706 10 dart firebase flutter google-cloud-firestore

我正在尝试对进入的列表视图进行排序。由于此列表视图是图像,因此我向 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"]),
                    ),
                  ),
                ),

              );
    }

          ))
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Dan*_* V. 16

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

你有:

CollectionReference collectionReference = Firestore.instance.collection("Events");
Run Code Online (Sandbox Code Playgroud)

使用orderBy,你应该有:

Query collectionReference = Firestore.instance.collection("Events").orderBy('field');
Run Code Online (Sandbox Code Playgroud)

orderBy应该返回 a Query,您不能再将其存储为 a CollectionReference

  • 如何使用“timeStamp”对元素进行排序? (4认同)