我正在尝试使用流构建器方法从Firestore中获取数据。最初,当我运行该应用程序时,我没有从firebase获得任何数据。我越来越
Invalid value: Valid value range is empty: 0
。
一旦我重新加载应用程序,数据就可用了
请找到下面的代码
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance
.collection('users/')
.where('uid', isEqualTo: _userUID)
.snapshots(),
builder: (BuildContext context, userSnapshot) {
if (!userSnapshot.hasData) return WidgetFunctions().loadingWidget();
return StreamBuilder(
stream: Firestore.instance
.collection('products')
.where('latest', isEqualTo: true)
.snapshots(),
builder: (cuserSnapshotontext, snapshot) {
if (!snapshot.hasData) return WidgetFunctions().loadingWidget();
if (snapshot.data.documents.length == 0)
return const Center(
child: Text(
"Not Available",
style: TextStyle(fontSize: 30.0, color: Colors.grey),
),
);
if (!userSnapshot.data.documents[0]['productViewPermission']) {
print('place6');
return const Center(
child: Text(
'You dont have permission to view products \n please contect us',
style: TextStyle(
fontSize: 18.0,
color: Colors.red,
fontWeight: FontWeight.bold),
));
}
return GridView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot.data.documents.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (BuildContext context, int index) {
return SingleProduct(
productCatagory: snapshot.data.documents[index]
['productCatogary'],
productName: snapshot.data.documents[index]
['productName'],
imageURL: snapshot.data.documents[index]['imageURLS']
[0],
price: userSnapshot.data.documents[0]
['priceViewpermission']
? snapshot.data.documents[index]['price'].toDouble()
: "To view price please contect us",
discountBool: snapshot.data.documents[index]
['discount'],
discountValue: snapshot.data.documents[index]
['discountValue'],
index: index,
description: snapshot.data.documents[index]
['description'],
make: snapshot.data.documents[index]['make'],
karat: snapshot.data.documents[index]['karat'],
waight:
snapshot.data.documents[index]['waight'].toDouble(),
condition: snapshot.data.documents[index]['condition'],
populer: snapshot.data.documents[index]['populer'],
isAvailable: snapshot.data.documents[index]
['isAvailable']);
},
);
});
});
}
I/flutter ( 3686): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3686): The following RangeError was thrown building StreamBuilder<QuerySnapshot>(dirty, state:
I/flutter ( 3686): _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#02fde):
I/flutter ( 3686): RangeError (index): Invalid value: Valid value range is empty: 0
I/flutter ( 3686): When the exception was thrown, this was the stack:
I/flutter ( 3686): #0 List.[] (dart:core-patch/growable_array.dart:145:60)
I/flutter ( 3686): #1 _PopularProductsContainerState.build.<anonymous closure>.<anonymous closure> (package:thaya_factory/Components/HomePageComponents/CategoryComponent/LatestProductsComponent.dart:54:49)
I/flutter ( 3686): #2 StreamBuilder.build (package:flutter/src/widgets/async.dart:425:74)
I/flutter ( 3686): #3 _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:125:48)
I/flutter ( 3686): #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4012:27)
I/flutter ( 3686): #5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15)
I/flutter ( 3686): #6 Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
I/flutter ( 3686): #7 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2340:33)
I/flutter ( 3686): #8 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:700:20)
I/flutter ( 3686): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5)
I/flutter ( 3686): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1016:15)
I/flutter ( 3686): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:958:9)
I/flutter ( 3686): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:874:5)
I/flutter ( 3686): #16 _invoke (dart:ui/hooks.dart:236:10)
I/flutter ( 3686): #17 _drawFrame (dart:ui/hooks.dart:194:3)
I/flutter ( 3686): (elided 3 frames from package dart:async)
I/flutter ( 3686): ═══════════════════════
请帮助我解决此问题 预先感谢
答案 0 :(得分:0)
首先,此代码块的语法无效:
if (snapshot.data.documents.length == 0)
return const Center(
child: Text(
"Not Available",
style: TextStyle(fontSize: 30.0, color: Colors.grey),
),
);
您忘记了if语句的左括号和右括号。
其次,在第二个if语句上尝试使用else if
,因为现在,当snapshot.data.documents.length
返回null
时,尽管列表是空,这就是您的!userSnapshot.data.documents[0]
的原因。
所以尝试一下:
RangeError
答案 1 :(得分:0)
这是因为 GridView 的 itemCount 没有访问数据快照。
尝试做
<块引用>itemCount = snapshot.data== null ? 0:snapshot.data.documents.length;