我正在尝试使用 Hive
然而,在Flutter中,我得到了一个距离误差。
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');
}
今天对我来说是星期六 time.weekday
对我来说是6,但是当我试着 print(stepCountList[6])
```
RangeError (index): Invalid value: Only valid value is 0: 6
1条答案
按热度按时间iklwldmw1#
你不能使用
insert()
方法在固定长度列表中,固定长度的意思是当您这样声明它时List<int> stepCountList = List(7);
编辑了代码,现在应该可以了