Flutter FutureBuilder 和 StreamBuilder 给出 RangeError Value not in range: 3 error in flutter Web but works well in app

Flutter FutureBuilder and StreamBuilder gives RangeError Value not in range: 3 error in flutter Web but works well in app

这是从firestore查询用户评分/评论的代码。它在 playstore 上的应用程序中运行良好,但在 flutter web 中出错。任何解决方案,因为除了范围错误外,终端中没有显示更多错误。

Future<void> getReview(String productId) async {
     final QuerySnapshot snapProducts = await FirebaseFirestore.instance
      .collection('products')
      .doc(productId)
      .collection('reviews')
      .get();
       return snapProducts;
     }

FutureBuilder(
  future: getReview(widget.products.id),
  builder: (context, snapshot) {
   if (snapshot.hasData) {
    final documents = snapshot.data.docs;
     final sum = documents.isEmpty
      ? 0.0
   : documents
      .map((m) => m['rating'])
        .reduce((a, b) => a + b) /
         documents.length;
        return Text(
           '$sum'.substring(0, 3),
           style: TextStyle(
           color: themeChange.isDarkTheme
                  ? Colors.white : Colors.black,
           ),
         ),                
     }
      return const Center(child: Text('...'));
      },
    ), 
  

从您的代码中删除 substring(0, 3)。它是一个 int,除非它大于 9999,否则它会一直抛出这个错误。