如何在 Hive (Flutter) 中存储列表?
How to store a List in Hive (Flutter)?
我正在尝试在 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
您不能在固定长度列表上使用 insert()
方法,固定长度是指当您以这种方式声明它时 List<int> stepCountList = List(7);
编辑了代码,现在它应该可以工作了
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');
}
}
我正在尝试在 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
您不能在固定长度列表上使用 insert()
方法,固定长度是指当您以这种方式声明它时 List<int> stepCountList = List(7);
编辑了代码,现在它应该可以工作了
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');
}
}