为什么它显示“getter 'exists' 被调用为 null”?

时间:2021-04-18 09:38:44

标签: android firebase flutter google-cloud-firestore flutter-test

我正在尝试使用 Firebase 在我的 Flutter 应用程序中制作一个最喜欢的按钮。但是,当我使用 snapshot.hasData 查看特定项目是否已存在于收藏夹列表中时,它始终返回 true,即使该项目不存在于数据库中。所以我尝试了 snapshot.data.exists 并且它有效。但是,即使应用程序运行良好”,它总是在调试控制台中显示以下错误:

The getter 'exists' was called on null.
Receiver: null
Tried calling: exists

我的完整代码:

Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot>(
      stream: FirebaseFirestore.instance
            .collection("UserData")
            .doc(_auth.currentUser.uid)
            .collection("Favourites")
            .doc(widget.items["name"])
            .snapshots(),
      builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        return Scaffold(
              body: Row(
                          children: [
                            snapshot.data.exists
                                ? Expanded(
                                    child: TextButton.icon(
                                      onPressed: () {
                                        FirebaseFirestore.instance
                                            .collection("UserData")
                                            .doc(_auth.currentUser.uid)
                                            .collection("Favourites")
                                            .doc(widget.items["name"])
                                            .delete();
                                      },
                                      label: Text(
                                        "Unfavourite Item",
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            color: Theme.of(context).accentColor),
                                      ),
                                      icon: Icon(
                                        Icons.star,
                                        color: Theme.of(context).accentColor,
                                      ),
                                      style: TextButton.styleFrom(
                                          minimumSize: Size.fromHeight(50),
                                          elevation: 0),
                                    ),
                                  )
                                : Expanded(
                                    child: TextButton.icon(
                                    onPressed: () {
                                      FirebaseFirestore.instance
                                          .collection("UserData")
                                          .doc(_auth.currentUser.uid)
                                          .collection("Favourites")
                                          .doc(widget.items["name"])
                                          .set({
                                        "name": widget.items["name"],
                                        "image": widget.items["image"],
                                        "price": widget.items["price"],
                                        "locate": widget.items["locate"],
                                        "assorted": true
                                      });
                                    },
                                    label: Text(
                                      "Favourite Item",
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          color: Theme.of(context).accentColor),
                                    ),
                                    icon: Icon(
                                      Icons.star_border,
                                      color: Theme.of(context).accentColor,
                                    ),
                                    style: TextButton.styleFrom(
                                        minimumSize: Size.fromHeight(50),
                                        elevation: 0),
                                  )),
                          ],
                        ),
        );
      }
    );
  }

请帮忙。我是 flutter 和 firebase 的新手。

1 个答案:

答案 0 :(得分:1)

当时您的快照为空。所以像这样处理

builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
      
      if (snapshot.connectionState == ConnectionState.done){
          return Scaffold(
            body: Row(
              children: [
                snapshot.data.exists
                    ? Expanded(
                        child: TextButton.icon(
                          onPressed: () {
                            FirebaseFirestore.instance
                                .collection("UserData")
                                .doc(_auth.currentUser.uid)
                                .collection("Favourites")
                                .doc(widget.items["name"])
                                .delete();
                          },
                          label: Text(
                            "Unfavourite Item",
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Theme.of(context).accentColor),
                          ),
                          icon: Icon(
                            Icons.star,
                            color: Theme.of(context).accentColor,
                          ),
                          style: TextButton.styleFrom(
                              minimumSize: Size.fromHeight(50), elevation: 0),
                        ),
                      )
                    : Expanded(
                        child: TextButton.icon(
                        onPressed: () {
                          FirebaseFirestore.instance
                              .collection("UserData")
                              .doc(_auth.currentUser.uid)
                              .collection("Favourites")
                              .doc(widget.items["name"])
                              .set({
                            "name": widget.items["name"],
                            "image": widget.items["image"],
                            "price": widget.items["price"],
                            "locate": widget.items["locate"],
                            "assorted": true
                          });
                        },
                        label: Text(
                          "Favourite Item",
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Theme.of(context).accentColor),
                        ),
                        icon: Icon(
                          Icons.star_border,
                          color: Theme.of(context).accentColor,
                        ),
                        style: TextButton.styleFrom(
                            minimumSize: Size.fromHeight(50), elevation: 0),
                      )),
              ],
            ),
          );
      }else{
          return SizedBox(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: const Center(
              child: CircularProgressIndicator(),
            ),
          );
      }
}