Gau*_*ari 5 sharedpreferences dart flutter
List<String> currentList =new List<String>();
Run Code Online (Sandbox Code Playgroud)
void initState() {
super.initState();
currentList=[];
}
Run Code Online (Sandbox Code Playgroud)
Future<Null> savePreferences(option,questionIndex) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
currentList.insert(questionIndex, option);
}
Run Code Online (Sandbox Code Playgroud)
所以基本上我想要做的是在共享首选项中为指定索引(我检查并正确返回索引)保存一个问题的选项。当它运行并且我按下该选项时,它返回给我以下错误:
E/flutter ( 6354): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Valid alue range is empty: 0
Run Code Online (Sandbox Code Playgroud)
我使用 insert 方法而不是 add 方法的原因是因为我想基本上替换已经存储在索引中的值,以防用户想要覆盖他们以前的答案。有人可以帮忙吗?谢谢
jam*_*lin 10
如果你想要一些像稀疏数组一样的东西,你可以使用 aMap代替。如果您希望能够按数字索引(而不是按插入顺序)按顺序迭代项目,则可以使用SplayTreeMap.
例如:
import 'dart:collection';
void main() {
final sparseList = SplayTreeMap<int, String>();
sparseList[12] = 'world!';
sparseList[3] = 'Hi';
sparseList[3] = 'Hello';
for (var entry in sparseList.entries) {
print('${entry.key}: ${entry.value}');
}
}
Run Code Online (Sandbox Code Playgroud)
印刷:
3: Hello
12: world!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1257 次 |
| 最近记录: |