首页 > 解决方案 > 使用 Future Builder 检查 firestore 是否有文档

问题描述

完整代码

class yourBookings extends StatefulWidget {
  @override
  _yourBookingsState createState() => _yourBookingsState();
}

class _yourBookingsState extends State<yourBookings> {
  StateModel appState;
  bool _loadingVisible = false;

  @override
  Widget build(BuildContext context) {
    appState = StateWidget.of(context).state;
    final number = appState?.user?.number ?? '';

    Future getPosts() async {
      var firestore = Firestore.instance;
      QuerySnapshot qn = await firestore
          .collection("confirmed_c_rides2")
          .document(number)
          .collection("1")
          .getDocuments();
      return qn.documents;
    }

    return Scaffold(
      appBar: AppBar(
        title: Text("Your Bookings :"),
      ),
      body: Container(
        child: FutureBuilder(
            future: getPosts(),
            builder: (_, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Center(
                  child: Text("Loading ..."),
                );
              } else if (snapshot.hasData) {
                return ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (_, index) {
                      Widget image_carousel = new Container(
                        height: 200.0,
                        child: new Carousel(
                          //borderRadius: BorderRadius.all(Radius.circular(2.0)),
                          boxFit: BoxFit.fitHeight,
                          images: [
                            Image.network(
                                "${snapshot.data[index].data["driverImage"]}"),
                            Image.network(
                                "${snapshot.data[index].data["carImage"]}")
                          ],
                          autoplay: true,
                          animationCurve: Curves.fastOutSlowIn,
                          animationDuration: Duration(milliseconds: 1000),
                          dotSize: 4.0,
                          indicatorBgPadding: 6.0,
                          dotBgColor: Colors.transparent,
                        ),
                      );
                      return Card(
                        child: ListTile(
                          title: Column(
                            children: <Widget>[
                              SizedBox(height: 10),
                              Text(
                                "Status:  ${snapshot.data[index].data["status"]}",
                                style: TextStyle(fontWeight: FontWeight.bold),
                              ),
                              SizedBox(height: 10),
                              image_carousel,
                              Text(
                                  "Name:  ${snapshot.data[index].data["driverName"]}"),
                              SizedBox(height: 10),
                              Text(
                                  "Gender:  ${snapshot.data[index].data["gender"]}"),
                              SizedBox(height: 10),
                              Text(
                                  "Experience:  ${snapshot.data[index].data["experience"]}"),
                              SizedBox(height: 10),
                              Text(
                                  "Number:  ${snapshot.data[index].data["driverNumber"]}"),
                              SizedBox(height: 10),
                              Text(
                                  "Time:  ${snapshot.data[index].data["time"]}"),
                              SizedBox(height: 10),
                              Text(
                                  "Scheduled on:  ${snapshot.data[index].data["rideOn"]}"),
                              SizedBox(height: 10),
                              RaisedButton(
                                color: Colors.black,
                                onPressed: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (_) => new issue()));
                                },
                                child: Text(
                                  "Having issue",
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                              SizedBox(height: 10),
                              RaisedButton(
                                onPressed: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (_) => rating1()));
                                },
                                child: Text("Rate driver"),
                              )
                            ],
                          ),
                        ),
                      );
                    });
              } else if (snapshot==null) {
                return Center(
                  child: Text("not found..."),
                );
              }
              return Center(
                child: Text("not found 2..."),
              );
            }),
      ),
    );
  }
}

getPosts() 指的是获取数据的 Firestore 位置。

我想使用 Future Builder 来检查 firestore 是否包含数字作为文档。我该怎么做?

number -> 1 包含更多细节。

如果数字不存在,则显示来自 firestore 的数据,否则显示“未找到...”。我该怎么做?

Future builder 用于流式传输 Firestore。

标签: fluttergoogle-cloud-firestore

解决方案


推荐阅读