Bruno Jurkovic
Bruno Jurkovic

Reputation: 35

How would I fix "RangeError (index): Invalid value: Not in range 0..13, inclusive: 14"

I have a list of matches(50), from which I need to extract a FlSpot() value, which takes an X, and Y as a parameter(doubles), but, my graph only takes 10 values in the X direction, so to combat that I added a for loop that removes extra matches and only returns 10 matches, but, the for loop throws an error.

I tried to tweak with the for loop to no avail.

    double calculateOneKDR(Matches match) {
    int totalKills = 0;
    int totalMatches = 0;

    totalKills += match.kills;
    totalMatches += match.matches;
    return num.parse((totalKills / totalMatches).toStringAsFixed(2));
  }

  List<FlSpot> calculateGraphDots(String playlist) {
    List<FlSpot> output = [];

    List<Matches> matches = _matches;
    List<Matches> validMatches = [];
    int extraLength = 0;

    matches.forEach(
      (match) {
        if (match.playlist == playlist) {
          validMatches.add(match);
        }
      },
    );
    if (validMatches.length > 10) {
      extraLength = validMatches.length - 10;
    }
    for (var i = 0; i < extraLength; i++) {
      validMatches.removeAt(i); //error point
    }
    List<double> matchkdrs = [];
    validMatches.forEach(
      (match) {
        matchkdrs.add(
          calculateOneKDR(match),
        );
      },
    );

    for (var i = 0; i < validMatches.length; i++) {
      output.add(
        FlSpot(
          double.parse('$i'),
          matchkdrs[i],
        ),
      );
    }
    return [...output];
  }

It should return a List<FlSpot> which has a length of 10, but sometimes it throws that error,

E/flutter (15859): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: RangeError (index): Invalid value: Not in range 0..13, inclusive: 14
E/flutter (15859): #0      List.[]  (dart:core-patch/growable_array.dart:147:60)
E/flutter (15859): #1      List.removeAt  (dart:core-patch/growable_array.dart:28:22)
E/flutter (15859): #2      PlayerStats.getMaxY 
package:fortbuddy/providers/player_stats.dart:237
E/flutter (15859): #3      _ItemShopScreenState.initState.<anonymous closure> 
package:fortbuddy/screens/item_shop_screen.dart:33
E/flutter (15859): #4      new Future.delayed.<anonymous closure>  (dart:async/future.dart:316:39)
E/flutter (15859): #5      _rootRun  (dart:async/zone.dart:1120:38)
E/flutter (15859): #6      _CustomZone.run  (dart:async/zone.dart:1021:19)
E/flutter (15859): #7      _CustomZone.runGuarded  (dart:async/zone.dart:923:7)```

Upvotes: 1

Views: 1353

Answers (2)

Razvan Curcudel
Razvan Curcudel

Reputation: 172

You can use sublist method. https://api.dartlang.org/stable/2.6.0/dart-core/List/sublist.html

So, you can write the if statement like this

if (validMatches.length > 10) {
     validMatches = validMatches.sublist(0, 10);
}    

and you don't need the for anymore

Upvotes: 2

Aganju
Aganju

Reputation: 6405

Everytime you remove removeAt(i), the rest of the vector moves up, so it is now shorter.
Therefore, your removes go over the end of it after a while.

You need to either remove at same index everytime, or start from the back - or use a different approach than removing one-by-one and moving the rest up every time, that is quite inefficient. Read up on erase.

Upvotes: 0

Related Questions