簡體   English   中英

如何按firestore flutter中的字段內容訂購文檔

[英]How to order documents by a field content in firestore flutter

我有一個酒店房間的集合。 當我添加一個新房間或顯示房間列表時,它們不會從 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);
          },
        ),
      ),
    ),
  );
}

如果您想從以下位置訂購結果:

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

使用 .orderBy() 函數。

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

如果你想過濾結果,你也可以使用 .where 函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM