小编Gau*_*ari的帖子

如何将特定索引处的值添加到 dart 中的空列表中?

  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 方法的原因是因为我想基本上替换已经存储在索引中的值,以防用户想要覆盖他们以前的答案。有人可以帮忙吗?谢谢

sharedpreferences dart flutter

5
推荐指数
1
解决办法
1257
查看次数

标签 统计

dart ×1

flutter ×1

sharedpreferences ×1