How to store a List in Hive (Flutter)?

3,123

You can't use insert() method on fixed length List, by fixed length I mean when you declare it this way List<int> stepCountList = List(7); Edited the code, now it should work

void _onData(int newValue) async {
    fetchSteps();
    steps = stepCountList[time.weekday] ?? 0;
    stepDivider += newValue;
    //stepCountList.insert(time.weekday - 1, steps);this is not correct
    stepCountList[time.weekday -1] = steps; // this should be the approach
     moneyLottoBox.put('stepCountList', stepCountList);
  }


void fetchSteps() {
    stepCountList = moneyLottoBox.get('stepCountList');
    if (stepCountList == null) {
     /* moneyLottoBox.put('stepCountList', <int>[7]); this statement is the 
reason for your error because your are storing a growable list instead of fixed list in the hive */

moneyLottoBox.put('stepCountList', List<int>(7));// try this instead 
// or u can use this moneyLottoBox.put('stepCountList', stepCountList);
      stepCountList = moneyLottoBox.get('stepCountList');
    }
  }
Share:
3,123
Arnav
Author by

Arnav

Flutter developer

Updated on December 22, 2022

Comments

  • Arnav
    Arnav over 1 year

    I am trying to store and fetch a list using Hive in Flutter, however I get a range error.

        int steps = 0;
        List<int> stepCountList = List(7);
        var time = DateTime.now();
    
       // This is my method for a listener that updates when it detects a change,
        void _onData(int newValue) async {
        fetchSteps();
        steps = stepCountList[time.weekday] ?? 0;
        stepDivider += newValue;
        stepCountList.insert(time.weekday - 1, steps);
         moneyLottoBox.put('stepCountList', stepCountList);
      }
    
     void fetchSteps() {
        stepCountList = moneyLottoBox.get('stepCountList');
        if (stepCountList == null) {
          moneyLottoBox.put('stepCountList', <int>[7]);
          stepCountList = moneyLottoBox.get('stepCountList');
        }
      }
    // I create my MoneyLotto box here,
    var moneyLottoBox = Hive.box('moneyLottoBox');
    Future<void> main async {
     moneyLottoBox = await Hive.openBox('box');
    }
    

    Today being Saturday for me, the value of time.weekday for me is 6, however it shows me the error when I try to print(stepCountList[6])

    RangeError (index): Invalid value: Only valid value is 0: 6
    
    • dev-aentgs
      dev-aentgs almost 4 years
      How are you printing the List? Just asking, shouldn't List length be 7 considering 0 to 6 inclusive ?
    • Arnav
      Arnav almost 4 years
      It prints this error when I print stepCountList[6]
    • dev-aentgs
      dev-aentgs almost 4 years
      Ok, print its length first. print(stepCountList.length).
    • Arnav
      Arnav almost 4 years
      It prints the length as 1
    • dev-aentgs
      dev-aentgs almost 4 years
      stepCountList = moneyLottoBox.get('stepCountList', defaultValue: <int>[6]); This is overwriting the initial List
    • Arnav
      Arnav almost 4 years
      Should I remove the defaultValue?
    • dev-aentgs
      dev-aentgs almost 4 years
      Try removing. What is moneyLottoBox ?
    • Arnav
      Arnav almost 4 years
      So when I try to replace now.weekday with 0, it works flawlessly but not with at the index I want to store it at :(
    • dev-aentgs
      dev-aentgs almost 4 years
      When you initially create moneyLottoBox and put stepCountList , what is the length of that List ?
    • Arnav
      Arnav almost 4 years
      I only call stepCountList in my onData method, and the value is the same as my current list
    • dev-aentgs
      dev-aentgs almost 4 years
      Try adding the stepCountList with length 7 in moneyLottoBox when you first create moneyLottoBox.
    • Arnav
      Arnav almost 4 years
      I updated my question, didn't work
    • dev-aentgs
      dev-aentgs almost 4 years
      Where do you create moneyLottoBox ? At the time of creation itself add a stepCountList of length 7.
    • Arnav
      Arnav almost 4 years
      I updated my question, could you show a programmatic example?
    • dev-aentgs
      dev-aentgs almost 4 years
      Added below. Try and see if it works or gives any errors.