firebase streambuilder documentsnapshot上的错误状态

abithluo  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(97)

当我运行我的代码时,我在StreamBuilder上不断收到坏状态错误,说明DocumentSnapshot不存在。下面是StreamBuilder导致错误的代码:”

@override
  Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot>(
      stream: FirebaseFirestore.instance
          .collection("overallRating")
          .doc("overall")
          .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Container();
        } else {
          List<dynamic> ratings = snapshot.data!["ratings"];

          String average = (ratings.reduce((a, b) => a + b) / ratings.length)
              .toStringAsFixed(1);

          return Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text(
                "Overal Rating",
                style:
                    TextStyle(color: Colors.grey, fontWeight: FontWeight.bold),
              ),
              Text(
                average,
                style: Theme.of(context).textTheme.headline3,
              ),
              RatingBar.builder(
                initialRating: double.parse(average),
                minRating: 1,
                maxRating: 5,
                direction: Axis.horizontal,
                ignoreGestures: true,
                allowHalfRating: true,
                itemCount: 5,
                itemPadding: const EdgeInsets.symmetric(horizontal: 4.0),
                itemBuilder: (context, _) => const Icon(
                  Icons.star,
                  color: Colors.amber,
                ),
                onRatingUpdate: (rating) {
                  print(rating);
                },
              ),
              Text(
                "Based on ${ratings.length} reviews",
                style: const TextStyle(
                    color: Colors.grey, fontWeight: FontWeight.bold),
              ),
              const SizedBox(
                height: 10.0,
              ),
              Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  RatingCategory(
                    title: "Excellent",
                    color: Colors.teal,
                    percent: getPercentage(5, ratings),
                  ),
                  RatingCategory(
                    title: "Good",
                    color: Colors.lightGreen,
                    percent: getPercentage(4, ratings),
                  ),
                  RatingCategory(
                    title: "Average",
                    color: Colors.yellow,
                    percent: getPercentage(3, ratings),
                  ),
                  RatingCategory(
                    title: "Below Average",
                    color: Colors.orange,
                    percent: getPercentage(2, ratings),
                  ),
                  RatingCategory(
                    title: "Poor",
                    color: Colors.red,
                    percent: getPercentage(1, ratings),
                  ),
                ],
              )
            ],
          );
        }
      },
    );
  }

“StreamBuilder以前为我工作过,直到我更新了我的依赖项。我看了太多关于解决错误的教程,似乎都不起作用。

hgqdbh6s

hgqdbh6s1#

试试这样

@override
Widget build(BuildContext context) {
  return StreamBuilder<DocumentSnapshot>(
    stream: FirebaseFirestore.instance
        .collection("overallRating")
        .doc("overall")
        .snapshots(),
    builder: (context, snapshot) {
      if (snapshot.hasError) {
        return Text('Error: ${snapshot.error}');
      } else if (snapshot.connectionState == ConnectionState.waiting) {
        return CircularProgressIndicator();
      } else if (!snapshot.hasData) {
        return Container();
      } else {
        List<dynamic> ratings = snapshot.data!["ratings"];

        String average = (ratings.reduce((a, b) => a + b) / ratings.length)
            .toStringAsFixed(1);

        return Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            const Text(
              "Overal Rating",
              style:
                  TextStyle(color: Colors.grey, fontWeight: FontWeight.bold),
            ),
            Text(
              average,
              style: Theme.of(context).textTheme.headline3,
            ),
            RatingBar.builder(
              initialRating: double.parse(average),
              minRating: 1,
              maxRating: 5,
              direction: Axis.horizontal,
              ignoreGestures: true,
              allowHalfRating: true,
              itemCount: 5,
              itemPadding: const EdgeInsets.symmetric(horizontal: 4.0),
              itemBuilder: (context, _) => const Icon(
                Icons.star,
                color: Colors.amber,
              ),
              onRatingUpdate: (rating) {
                print(rating);
              },
            ),
            Text(
              "Based on ${ratings.length} reviews",
              style: const TextStyle(
                  color: Colors.grey, fontWeight: FontWeight.bold),
            ),
            const SizedBox(
              height: 10.0,
            ),
            Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                RatingCategory(
                  title: "Excellent",
                  color: Colors.teal,
                  percent: getPercentage(5, ratings),
                ),
                RatingCategory(
                  title: "Good",
                  color: Colors.lightGreen,
                  percent: getPercentage(4, ratings),
                ),
                RatingCategory(
                  title: "Average",
                  color: Colors.yellow,
                  percent: getPercentage(3, ratings),
                ),
                RatingCategory(
                  title: "Below Average",
                  color: Colors.orange,
                  percent: getPercentage(2, ratings),
                ),
                RatingCategory(
                  title: "Poor",
                  color: Colors.red,
                  percent: getPercentage(1, ratings),
                ),
              ],
            )
          ],
        );
      }
    },
  );
}

相关问题