【问题标题】:How to order documents by a field content in firestore flutter如何在 Firestore Flutter 中按字段内容对文档进行排序
【发布时间】:2021-09-10 22:25:01
【问题描述】:

我有一系列酒店的房间。当我添加一个新房间或显示房间列表时,它们不显示从 1 到 5。有没有办法按房间号排序?

房间集合中的文档自动生成id,每个房间都有一个名为房间号的字段(例如:1,2,3 ..)

final rooms = Provider.of<List<Room>>(context) ?? [];

return  StreamProvider<List<Room>>.value(
  initialData: [],
  value: DatabaseService().singleRoom,
  child: Scaffold(
    appBar: AppBar(
      title: Text("Liste des chambre single"),
      backgroundColor: Colors.blue,
      elevation: 2.0,
      leading: IconButton(icon: Icon(Icons.chevron_left),onPressed: ()=> Navigator.pushReplacementNamed(context, '/room_type_screen'),),
      actions: [
        ElevatedButton.icon(
          icon: Icon(Icons.home),
          label: Text(""),
          onPressed: ()=> Navigator.pushReplacementNamed(context, '/admin_home'),
        ),
        ElevatedButton.icon(
          icon: Icon(Icons.add),
          label: Text(""),
          onPressed: ()=> Navigator.pushNamed(context, '/add_room'),
        ),

      ],
    ),
    body: ListView.builder(
      itemCount: rooms.length,
      itemBuilder: (context,index){
        return RoomTile(room : rooms[index]);
      },
    ),
  ),
Widget build(BuildContext context) {
void deleteRoomFromDatabase(String id){
  final CollectionReference singleRoomCollection = FirebaseFirestore.instance.collection("rooms/single/room");
  singleRoomCollection.doc(id).delete();
}
  return Padding(
    padding: EdgeInsets.only(top: 10.0),
    child: Card(
      margin: EdgeInsets.fromLTRB(20, 6, 20, 0),
      child: GridTileBar(
        backgroundColor: Color(0xFFBDBDBD),
        leading: CircleAvatar(
          radius: 30.0,
          backgroundColor: Colors.orange,
          // backgroundImage: AssetImage('assets/6074.jpg'),
          child: Text("${room.numChambre.toString().substring(1)}",
            style: TextStyle(
              color: Colors.white,
              fontSize: 25.0,
              fontWeight: FontWeight.bold
            ),
          ),
        ),
        title: room.reserver?Text("Chambre réservé"):Text('Chambre disponible',style: TextStyle(fontSize: 20),),
        subtitle: Text("Prix : "+room.prix.toString()),
        trailing: IconButton(
          icon: Icon(Icons.delete),
          onPressed: (){
            deleteRoomFromDatabase(room.roomId);
          },
        ),
      ),
    ),
  );
}

【问题讨论】:

  • 你能分享一下你的数据库结构的截图吗?

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

如果您想从以下位置订购结果:

FirebaseFirestore.instance.collection("rooms/single/room")

使用 .orderBy() 函数。

FirebaseFirestore.instance.collection("rooms/single/room").orderBy("FieldInFirestoreToOrderBy", descending: true/false)

如果你想过滤结果,你也可以使用 .where 函数

【讨论】:

  • 好答案。据我从阅读问题中可以看出,要订购的字段是numChambre
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 2020-12-22
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2015-09-11
相关资源
最近更新 更多