1

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;
  }
}
4
  • iu think that you wana do if ( snapshot.data.insights == null || snapshot.data.insights.isEmpty) not if (insights == null || insights.isEmpty) Commented Sep 1, 2020 at 15:41
  • No that is not it, it was working fine it happens when I swipe on the cards a lot so it can't be that Commented Sep 1, 2020 at 15:46
  • yes because you tried to foreach insights even data not yet loaded so add if (snapshot.data != null ) if (insights == null || insights.isEmpty) { ...... Commented Sep 1, 2020 at 15:56
  • That is not it I think I fixed testing now Commented Sep 1, 2020 at 16:02

1 Answer 1

1

So I finally found out was causing the issue I have the following buildCardsList

My app is like tinder where you have cards and swipe

This is the list widget, Basically insights was negtive at some point just need to add if (insights.length - _nrCardsMax > 0)

List<Widget> buildCardsList(BuildContext context) {
    // List<Company> lastCompanies = companies.sublist(0, 5);
    int _nrCardsMax = 5;
    List<Widget> cards = [];

    double initialBottom = 15.0;
    double backCardPosition = initialBottom + (_nrCardsMax - 1) * 10 + 10;
    double backCardWidth = -10.0;
    print(insights.length);

    if (insights.length - _nrCardsMax > 0) {
      for (int i = insights.length - _nrCardsMax; i < insights.length; i++) {
        bool _isLastCard = i == insights.length - 1;
        if (!_isLastCard) {
          backCardPosition -= 10;
          backCardWidth += 10;
        }
        cards.add(swipeableCard(
          _isLastCard,
          insights[i],
          _isLastCard ? bottom.value : backCardPosition,
          _isLastCard ? right.value : 0.0,
          0.0,
          _isLastCard ? backCardWidth + 10 : backCardWidth,
          _isLastCard ? rotate.value : 0.0,
          _isLastCard ? rotate.value < -10 ? 0.1 : 0.0 : 0,
          context,
          dismissInsight,
          flag,
          saveInsight,
          saveContactedContact,
          swipeRight,
          swipeLeft,
        ));
      }
    }
    return cards;
  }

  @override
  bool get wantKeepAlive => true;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.