RangeError(索引):无效值:有效值范围为空:flutter Firestore中为0

时间:2020-11-11 14:03:20

标签: flutter dart google-cloud-firestore

将项目连接到Firestore并创建集合,文档等后,当我尝试在索引0处打印产品名称时,这就是我得到的错误。

在构建FutureBuilder(dirty,state:_FutureBuilderState#18c55)时引发了以下RangeError: RangeError(索引):无效值:有效值范围为空:0

I have read this similar question posted here but it does not solve my issue

以下是我的代码除外

class Home extends StatelessWidget {

Product menData;

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: _onBackPressed,
      child: Scaffold(
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.only(top: 10, left: 10, right: 10),
              child: FutureBuilder(
             future: FirebaseFirestore.instance.collection("products")
                      .doc("6di1OSBgwOjyLbPtl75g").collection("recent").get(),
            builder: (context, snapshot) {
              if(snapshot.connectionState == ConnectionState.waiting){
                return Center(
                  child: spinkit,
                );
              }
              menData = Product(
                image: snapshot.data.documents[0]["image"],
                name: snapshot.data.documents[0]["name"],
                price: snapshot.data.documents[0]["price"],
              );
              print(menData.name);

这是模型

导入“ package:flutter / material.dart”;

class Product {
  final String image;
  final String name;
  final double price;

  Product({@required this.image, @required this.name, @required this.price});

}

This is a shot of the database

2 个答案:

答案 0 :(得分:1)

FutureBuilder倾向于掩埋数据类型错误。您的通话FirebaseFirestore.instance.collection("products").doc("6di1OSBgwOjyLbPtl75g").collection("recent").get(),

中最有可能是问题

我猜测某些产品未加载正确的类型,

      final String image;
      final String name;
      final double price;  

double price可能以int的形式出现或类似的东西出现。我已经诊断出其中一些问题,它们几乎总是像您的错误一样出现:

使用此示例的“快照视图检查”来交换视图。它将验证您的错误并显示正确的错误:https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

List<Widget> children;
          if (snapshot.hasData) {
            children = <Widget>[
              Icon(
                Icons.check_circle_outline,
                color: Colors.green,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Result: ${snapshot.data}'),
              )
            ];
          } else if (snapshot.hasError) {
            children = <Widget>[
              Icon(
                Icons.error_outline,
                color: Colors.red,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Error: ${snapshot.error}'),
              )
            ];
          } else {
            children = <Widget>[
              SizedBox(
                child: CircularProgressIndicator(),
                width: 60,
                height: 60,
              ),
              const Padding(
                padding: EdgeInsets.only(top: 16),
                child: Text('Awaiting result...'),
              )
            ];
          }
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: children,
            ),
          );

答案 1 :(得分:1)

发生此错误 "Invalid value: Valid value range is empty: 0" 是因为您的列表索引值为空,但您必须强制调用该值。

尝试将这 3 行临时更改为硬编码值:

image: snapshot.data.documents[0]["image"]
name: snapshot.data.documents[0]["name"]
price: snapshot.data.documents[0]["price"]

to 

image: 'hardcode'
name: 'harcode'
price: 'hardcode'