【发布时间】:2020-10-17 21:34:57
【问题描述】:
我尝试在 Flutter 中使用 Hive 存储和获取列表,但出现范围错误。
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
【问题讨论】:
-
如何打印
List?只是问,考虑到 0 到 6,Listlength不应该是 7 吗? -
当我打印 stepCountList[6] 时它会打印这个错误
-
好的,
print它的长度优先。print(stepCountList.length). -
将长度打印为 1
-
stepCountList = moneyLottoBox.get('stepCountList', defaultValue: <int>[6]);这是覆盖最初的List
标签: flutter dart flutter-hive