Flutter 排序数据 Firestore 与 Streambuilder

标签 flutter google-cloud-firestore

我的目标:当用户按下“_mainListItem”中的“List”按钮时,我希望 ListView 按 orderBy 排序。以及在屏幕上更新

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class mainlist extends StatefulWidget {
  @override
  _mainlistpage createState() => _mainlistpage();
}

class _mainlistpage extends State<mainlist> {


  Widget homePage() {
    return StreamBuilder(
      stream: Firestore.instance.collection("Test").snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text("Loading");
        return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) =>
                _mainListItem(context, snapshot.data.documents[index]));
      },
    );
  }


  Widget _mainListItem(BuildContext context, DocumentSnapshot document) {
    return Card(
      color: Colors.white,
      child: InkWell(
        onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => profile(context, document)));
        },
        child: Container(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                    border: Border(bottom: BorderSide(color: Colors.black12))),
                child: Row(
                  children: [
                    Expanded(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Column(
                            children: <Widget>[
                              Stack(
                                  alignment: Alignment.topRight,
                                  children: <Widget>[
                                    Padding(
                                      padding: const EdgeInsets.only(right: 5),
                                      child: ClipRRect(
                                        borderRadius:
                                            BorderRadius.circular(0.0),
                                        child: FittedBox(
                                            child: Image.asset(
                                          "assets/Profile Picture.png",
                                          fit: BoxFit.fill,
                                        )),
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.only(
                                          top: 7, right: 4),
                                      child: Text(
                                        'Test',
                                        style: TextStyle(fontSize: 12),
                                      ),
                                    ),
                                  ]),
                              Row()
                            ],
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(
                                    document['name'],
                                  ),
                                  // Text("2km"),
                                ],
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(top: 5, bottom: 5),
                    child: Row(
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.only(left: 10, right: 7),
                          child: Container(
                            child: Material(
                              borderRadius: BorderRadius.circular(5),
                              shadowColor: Colors.black,
                              elevation: 1,
                              child: SizedBox(
                                  height: 28,
                                  width: 68,
                                  child: IconButton(
                                    padding: EdgeInsets.only(bottom: 10),
                                    **icon: Icon(Icons.list),
                                    disabledColor: Colors.blue,
                                    iconSize: 25,**
                                  )),
                            ),
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        backgroundColor: Colors.grey,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          color: Colors.red,
        ),
        title: Text("Test"),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.menu),
            iconSize: 30,
            color: Colors.white,
          )
        ],
      ),
      body: homePage(),
    );
  }
}


我试过了
- 将流构建器功能添加到 ontapped: 在列表按钮上
- 已经阅读并观看了所有视频,但仍然无法找到解决方案

注意:该应用程序看起来很奇怪,因为我删除了不必要的信息

最佳答案

您可以在快照方法之前对列表项进行排序,例如:

.orderBy('sortField', descending: true).snapshot()

我希望这对你有用。

关于Flutter 排序数据 Firestore 与 Streambuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58044290/

相关文章:

Flutter 是否可以在不使用 Cupertino 小部件的情况下将我的应用程序发布到 iOS App Store 上?

android - Flutter Bloc保持重建小部件而不改变状态

dart - 所有屏幕都需要一个持久/相同的底部导航栏 - Flutter

javascript - 如何在激活离线持久化时导出 Firestore?

firebase - 您可以在身份验证登录时触发(firebase)云功能(但不能创建帐户)吗?

firebase - 解析错误 : 'import' and 'export' may appear only with 'sourceType: module'

google-cloud-firestore - Firestore 唯一索引或唯一约束?

flutter - 如何将边框半径设置为导航栏 flutter

dart - http url参数无效字符错误

Angular 和 AngularFire 5 从文档中检索数据