繁体   English   中英

RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore

[英]RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore

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

以下 RangeError 被抛出 building FutureBuilder(dirty, state: _FutureBuilderState#18c55): RangeError (index): Invalid value: Valid value range is empty: 0

我已经阅读了这里发布的类似问题,但它并没有解决我的问题

以下是我的代码除外

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});

}

这是数据库截图

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,
            ),
          );

发生此错误"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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM