Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未处理的异常:类型';列表<;动态>';不是类型为';列表<;用户利益>';颤振中的共享偏好_Android_Ios_Flutter_Sharedpreferences - Fatal编程技术网

Android 未处理的异常:类型';列表<;动态>';不是类型为';列表<;用户利益>';颤振中的共享偏好

Android 未处理的异常:类型';列表<;动态>';不是类型为';列表<;用户利益>';颤振中的共享偏好,android,ios,flutter,sharedpreferences,Android,Ios,Flutter,Sharedpreferences,在我的应用程序中,我希望在共享首选项中存储对象列表,但当我从共享首选项中获取该数据时,我会得到未处理的异常: 类型List不是类型List 异常,未获取数据 这是我的密码 static void addInterests(List<UserInterests> interests) async { SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString('inte

在我的应用程序中,我希望在共享首选项中存储对象列表,但当我从共享首选项中获取该数据时,我会得到未处理的异常:

类型
List
不是类型
List

异常,未获取数据

这是我的密码

static void addInterests(List<UserInterests> interests) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString('interstsArray', jsonEncode(interests));
  }

  static Future<List<UserInterests>> getInterests() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<UserInterests> interests = jsonDecode(prefs.getString('interstsArray'));
    return interests;
  }

static void logoutUserSF() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove("interstsArray");
  }
static void addinterest(列表兴趣)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
prefs.setString('interssarray',jsonEncode(interest));
}
静态未来getInterests()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
列表兴趣=jsonDecode(prefs.getString('interssarray');
归还利息;
}
静态void logoutUserSF()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
优先移除(“间隙”);
}
第二行jsonDecode中的getInterests函数出错,请给出一些建议如何执行

jsonDecode()
字符串
转换为
列表
,它不知道
用户兴趣
类型。您必须自己编写一个函数/构造函数来转换它,例如:

UserInterests.fromJson(List<dynamic> json){
  firstMember = json['firstMember'];
  secondMember = json['secondMember'];
}
UserInterests.fromJson(列出json){
firstMember=json['firstMember'];
secondMember=json['secondMember'];
}
然后,您可以像这样使用它:

List<UserInterests> interests = [
  for(var json in jsonDecode(prefs.getString('interstsArray')))
     UserInterests.fromJson(json),
];
兴趣列表=[
for(jsonDecode中的var json(prefs.getString('interssarray'))
fromJson(json),
];

在我的UserInterests类中,我有工厂用户兴趣。fromJson(Map json)=>\u$UserInterestsFromJson(json);映射到JSON()=>$UserInterestsToJson(此);所以我把代码放在这里?如果你已经有了一个生成的代码,那么试着使用它,如果它失败了就修复它(因为有时候生成器不能正确地推断类型)。或者只是重写它,我想,你的选择:)