GesutreDetector onPressed 后 ListView 未更新

ListView not updating after GesutreDetector onPressed

我点击我的按钮后我的列表视图没有更新,即使函数调用(fillPickupList)用数据填充数组,列表视图仍然没有显示任何东西,我什至在异步后检查了数组的大小调用大小为 1 的 (fillpickupList) 函数。但是当我重新加载应用程序时,使用 hotreload 它会在重新加载应用程序之前临时显示数据,当我重新启动应用程序时它也会做同样的事情。

class OrdersPage extends StatefulWidget {
  final String coursecode;

  const OrdersPage({Key? key, required this.coursecode}) : super(key: key);

  @override
  _OrdersPageState createState() => _OrdersPageState();
}

class _OrdersPageState extends State<OrdersPage> {
  List<OrdersClass> pendingList = [];
  List<OrdersClass> clearedList = [];
  bool showPendingPickups = true;
  // Map<String,dynamic> eachDocument = {};

  void fillPickupList() async {
    pendingList = [];
    print("size of pending list ${pendingList.length}"); //this prints out zero
    await FirebaseFirestore.instance
        .collection('Computer science')
        .doc("orders")
        .collection(widget.coursecode.replaceAll(" ", "").trim().toLowerCase())
        .get()
        .then(
            (QuerySnapshot querySnapshot) => querySnapshot.docs.forEach((doc) {
                  Map<String, dynamic> eachDocument =
                      doc.data() as Map<String, dynamic>;
                  (eachDocument.isEmpty)
                      ? print(doc.id)
                      : this.pendingList.add(OrdersClass(
                          firstname: eachDocument["firstname"],
                          lastname: eachDocument["lastname"],
                          matnumber: eachDocument["matnumber"],
                          id: eachDocument["id"],
                          date: eachDocument["date"],
                          documentId: doc.id,
                          pending: true));
                }));
    print(
        "in pending list .. ${pendingList.first.firstname}"); //This prints out the firstname
  }

  void fillClearedList() async {
    // print (widget.coursecode.replaceAll(" ", "").trim().toLowerCase());
    await FirebaseFirestore.instance
        .collection('Computer science')
        .doc("clearedorders")
        .collection(widget.coursecode.replaceAll(" ", "").trim().toLowerCase())
        .get()
        .then(
            (QuerySnapshot querySnapshot) => querySnapshot.docs.forEach((doc) {
                  Map<String, dynamic> eachDocument =
                      doc.data() as Map<String, dynamic>;
                  (eachDocument.isEmpty)
                      ? print(doc.id)
                      : clearedList.add(OrdersClass(
                          firstname: eachDocument["firstname"],
                          lastname: eachDocument["lastname"],
                          matnumber: eachDocument["matnumber"],
                          id: eachDocument["id"],
                          date: eachDocument["date"],
                          documentId: doc.id,
                          pending: true));
                  // print (doc.id);
                }));
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    FirebaseFirestore.instance;

    // fillPickupList();
    // fillClearedList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        child: Column(
          children: [
            Container(
              height: 100,
              child: GridView.count(crossAxisCount: 2, children: [
                GestureDetector(
                  onTap: () {
                    setState(() {
                      this.showPendingPickups = true;
                      // pendingList = [];
                    });
                    fillPickupList();
                  },
                  child: Card(
                    child: Center(
                      child: Text(
                        "Pending Pickups",
                        style: TextStyle(
                            fontSize: 20,
                            fontStyle: FontStyle.italic,
                            color: Colors.blue),
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    setState(() {
                      showPendingPickups = false;
                    });
                    fillClearedList();
                  },
                  child: Card(
                    child: Center(
                      child: Text(
                        "Picked Up",
                        style: TextStyle(
                            fontSize: 20,
                            fontStyle: FontStyle.italic,
                            color: Colors.blue),
                      ),
                    ),
                  ),
                )
              ]),
            ),
            Padding(padding: EdgeInsets.only(bottom: 15)),
            (showPendingPickups)
                ? Text(
                    //prints this successfully
                    "Pending Pickups ",
                    style: TextStyle(
                      fontStyle: FontStyle.italic,
                      fontWeight: FontWeight.bold,
                      decoration: TextDecoration.underline,
                    ),
                  )
                : Text(
                    "Cleared Pickups",
                    style: TextStyle(
                      fontStyle: FontStyle.italic,
                      fontWeight: FontWeight.bold,
                      decoration: TextDecoration.underline,
                    ),
                  ),
            Padding(padding: EdgeInsets.only(bottom: 5)),
            (this.showPendingPickups)
                ? Container(
                    //does not print this on button tap.
                    //make this dynamic
                    height: 300,
                    child: ListView.separated(
                        itemBuilder: (context, index) {
                          // return ListTile(
                          //   leading:
                          //       Text("the $index"), //pendingList[index].date
                          //   title: Text(pendingList[index].firstname +
                          //       " " +
                          //       pendingList[index].lastname +
                          //       "\n" +
                          //       pendingList[index].matnumber),
                          //   subtitle: Text("userid: " + pendingList[index].id),
                          //   trailing: ElevatedButton(
                          //     onPressed: () async {
                          //       // moveUserToPickedUp(
                          //       //     index: index, date: pendingList[index].date, firstname: pendingList[index].firstname,
                          //       //     documentID: pendingList[index].documentId,
                          //       //     id: pendingList[index].id, lastname: pendingList[index].lastname,
                          //       //     matnumber: pendingList[index].matnumber);
                          //       // setState(() {
                          //       //
                          //       // });
                          //     },
                          //     child: Text("PickedUp"),
                          //   ),
                          // );
                          return Text("the $index");
                        },
                        separatorBuilder: (context, index) {
                          return Divider();
                        },
                        itemCount: pendingList.length),
                  )
                : Container(
                    height: 300,
                    child: ListView.separated(
                        itemBuilder: (context, index) {
                          return ListTile(
                            leading:
                                Text("cleared list"), //clearedList[index].date
                            title: Text(clearedList[index].firstname +
                                " " +
                                clearedList[index].lastname +
                                "\n" +
                                clearedList[index].matnumber),
                            subtitle: Text("userid: " + clearedList[index].id),
                            trailing: ElevatedButton(
                              onPressed: () async {
                                // moveUserToPickedUp(
                                //     index: index, date: pendingList[index].date, firstname: pendingList[index].firstname,
                                //     documentID: pendingList[index].documentId,
                                //     id: pendingList[index].id, lastname: pendingList[index].lastname,
                                //     matnumber: pendingList[index].matnumber);
                                // setState(() {
                                //
                                // });
                              },
                              child: Text("PickedUp"),
                            ),
                          );
                        },
                        separatorBuilder: (context, index) {
                          return Divider();
                        },
                        itemCount: clearedList.length),
                  )
          ],
        ),
      ),
    );
  }
}

如果您的目标是在新数据添加到 Firestore 时立即从 Firestore 接收数据,那么我建议改用 Streambuilder。那么您将在写入 Firestore 后立即收到数据。

示例:

     class OrdersPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final user = FirebaseAuth.instance.currentUser!;
    
        return StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection('Computer science')
                .doc('orders')
                .collection('sub-collection')
                .snapshots(),
            builder: (ctx, AsyncSnapshot<QuerySnapshot> ordersSnapshot) {
           
    
              final currentOrders = ordersSnapshot.data!.docs;
    
             ListView.builder(
                  itemCount: currentOrders.length,
                  itemBuilder: (ctx, index) => OrderItem(
                      OrdersClass(
                          firstname: currentOrders[index]["firstname"],
                          lastname: currentOrders[index]["lastname"],
                          matnumber: currentOrders[index]["matnumber"],
                          id: currentOrders[index]["id"],
                          date: currentOrders[index]["date"],
                          documentId: doc.id,
                          pending: true));
                      ),
                    );
            });
      }
    }