I'm getting the following error
The following RangeError was thrown building StreamBuilder<InsightsModel>(dirty, state:
I/flutter (14582): _StreamBuilderBaseState<InsightsModel, AsyncSnapshot<InsightsModel>>#364a0):
I/flutter (14582): RangeError (index): Invalid value: Not in inclusive range 0..3: -1
I can't figure out causing and or how to fix it
It's coming from
The relevant error-causing widget was: I/flutter (14582): StreamBuilder
Here is the code for that widget
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
body: Container(
alignment: Alignment.center,
child: Center(
child: StreamBuilder(
stream: widget.allInsights,
builder: (context, AsyncSnapshot<InsightsModel> snapshot) {
if (snapshot.hasData) {
if (insights == null || insights.isEmpty) {
snapshot.data.insights.forEach((element) {
if (prefs.getInt(
'swiped_insight_${element.id.toString()}') ==
null) {
insights.add(element);
}
});
}
return Stack(
alignment: AlignmentDirectional.center,
children: buildCardsList(context),
);
} else {
return Center(child: CircularProgressIndicator());
}
}),
)),
);
}
and here is the code for InsightsModel
class InsightsModel {
List<Insight> _insights = [];
InsightsModel.fromJson(List<dynamic> json) {
print('_insights');
print(_insights);
for (int i = 0; i < json.length; i++) {
_insights.add(Insight.fromJson(json[i]));
}
}
List<Insight> get insights => _insights;
List<Insight> getInsightsByLimit(int limit) {
List<Insight> list = [];
for (int x = 0; x < limit && x < _insights.length; x++) {
list.add(_insights[x]);
}
return list;
}
}