我正在try 对传入的列表视图进行排序.因为这个列表视图是图像,所以我向FireStore对象添加了一个数字,这样我就可以升序排序.

我似乎不能让项目排序,我确定这是我如何构建的应用程序.

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

              );
    }

          ))
        ],
      ),
    );
  }
}

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

推荐答案

我认为这只是一个简单的疏忽导致的类型错误.(请下次为你的应用程序添加错误 case ,我在这里猜测.)

您必须:

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

使用orderBy,您应该拥有:

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

如果orderBy应该返回Query,则不能再将其存储为CollectionReference.

Dart相关问答推荐

为具有通用函数字段的Generic类实现copyWith

有没有办法否定IF条件中的模式大小写匹配?

`异步内联方法`的这些定义之间有什么区别?

Flutter 布局:如何在 Flutter 中将 Row 放入 Flex(或另一个 Row)中?还使用堆栈小部件

Flutter Expansion Tile -- 标题 colored颜色 变化和尾随动画箭头 colored颜色 变化

SliverList / SliverChildBuilderDelegate 提供初始索引或允许负索引

如何在 Flutter 包/插件开发中添加assets?

替换 Flutter 中的片段等小部件

Flutter 屏幕尺寸计算

是否有必要为Flatter安装Android Studio,或者可以用什么替代Android Studio?

如何在 flutter 中对json进行字符串化

如何在没有脚手架的情况下显示snackBar

Visual Studio 的 Dart 插件

如何在 Dart 中创建我们自己的metadata元数据?

在 Dart 中将类类型作为变量传递

如何在 Dart 中以正确的方式重定向和重新加载?

如何在dart中生成随机字符串?

Dart 中的 List.shuffle()?

如何在 Dart 中定义接口?

List firstWhere Bad state: No element