Flutter Firestore StreamBuilder getting null value initially
2,787
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.
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
.
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),
)
);
}
Author by
vinu vish
Updated on December 13, 2022Comments
-
vinu vish over 1 year
I'm trying to get data from firestore by using stream builder method. initially, when I run the app I don't get any data from firebase. I'm getting
Invalid value: Valid value range is empty: 0
. once I reload app the data is available please find the code bellowWidget 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
-
vinu vish over 4 yearsThanks for the reply I have tried still I'm getting same error and Note** when I reload the app it's working fine. the error occurs only for the first-time launch