[英]RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore
After connecting my project to Firestore and creating collection, document, etc, when I try to print the name of a product at index 0, this is the error I am getting.将我的项目连接到 Firestore 并创建集合、文档等后,当我尝试在索引 0 处打印产品名称时,这是我遇到的错误。
The following RangeError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#18c55): RangeError (index): Invalid value: Valid value range is empty: 0以下 RangeError 被抛出 building FutureBuilder(dirty, state: _FutureBuilderState#18c55): RangeError (index): Invalid value: Valid value range is empty: 0
I have read this similar question posted here but it does not solve my issue 我已经阅读了这里发布的类似问题,但它并没有解决我的问题
Below is except of my code以下是我的代码除外
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);
This is the model这是模型
import 'package:flutter/material.dart';导入 '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 tends to bury data type errors. FutureBuilder 倾向于隐藏数据类型错误。 It's most likely an issue within your call
FirebaseFirestore.instance.collection("products").doc("6di1OSBgwOjyLbPtl75g").collection("recent").get(),
这很可能是您调用
FirebaseFirestore.instance.collection("products").doc("6di1OSBgwOjyLbPtl75g").collection("recent").get(),
I'm guessing that some product is not loading with the proper types,我猜有些产品没有加载正确的类型,
final String image;
final String name;
final double price;
Probably the double
price
is coming in as a int
or something weird like that.可能
double
price
是作为int
或类似的东西出现的。 I've diagnosed a few of these issues and they almost always present like your bug:我已经诊断出其中一些问题,它们几乎总是像您的错误一样出现:
Swap out your view with this example's "snapshot view check".使用此示例的“快照视图检查”交换您的视图。 It will validate your error and display the correct one: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
它将验证您的错误并显示正确的错误: 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,
),
);
This error "Invalid value: Valid value range is empty: 0"
happened because your list index value is null, but you are keep force to call the value out.发生此错误
"Invalid value: Valid value range is empty: 0"
是因为您的列表索引值为空,但您必须强制调用该值。
Try to temporary change this 3 lines to hardcode value:尝试将这 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.