[英]Flutter Firestore StreamBuilder getting null value initially
I'm trying to get data from firestore by using stream builder method.我正在尝试使用流构建器方法从 Firestore 获取数据。 initially, when I run the app I don't get any data from firebase.
最初,当我运行该应用程序时,我没有从 firebase 获取任何数据。 I'm getting
Invalid value: Valid value range is empty: 0
.我得到
Invalid value: Valid value range is empty: 0
。 once I reload app the data is available please find the code bellow一旦我重新加载应用程序数据可用请找到下面的代码
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): ═══════════════════════
Please help me to fix the issue Thanks in advance请帮我解决问题提前致谢
Well first of all, this block of code has invalid syntax:首先,这段代码的语法无效:
if (snapshot.data.documents.length == 0)
return const Center(
child: Text(
"Not Available",
style: TextStyle(fontSize: 30.0, color: Colors.grey),
),
);
You forgot the opening and closing brackets on the if statement.您忘记了 if 语句中的左括号和右括号。
And secondly, try an else if
on the second if statement, because now, when snapshot.data.documents.length
returns null
, it will still check for !userSnapshot.data.documents[0]
, though the list is empty, which is the cause for your RangeError
.其次,在第二个 if 语句上尝试
else if
,因为现在,当snapshot.data.documents.length
返回null
,它仍然会检查!userSnapshot.data.documents[0]
,尽管列表是空的,这是您的RangeError
的原因。
So try this:所以试试这个:
if (snapshot.data.documents.length == 0) {
return const Center(
child: Text(
"Not Available",
style: TextStyle(fontSize: 30.0, color: Colors.grey),
),
);
} else 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),
)
);
}
This is because itemCount of GridView is accessing no data snaphsot.这是因为 GridView 的 itemCount 没有访问数据快照。
Try doing尝试做
itemCount = snapshot.data== null ?
itemCount = snapshot.data== null ? 0 : snapshot.data.documents.length;
0 : 快照.数据.文档.长度;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.