将用户图标输入保存到共享首选项
Saving the users icon input to shared preferences
我仍在努力将 iconData 保存到我的待办应用程序的设备存储中,我收到此错误:
Converting object to an encodable object failed: Instance of 'IconData'
在调试控制台中添加待办事项。
如果我取出 iconData,待办事项项会正确保存,当我将其放回原处时,会出现该错误。
import 'package:flutter/foundation.dart';
class ToDo{
final String id;
final String title;
final IconData icon;
const ToDo({
@required this.id,
@required this.title,
@required this.icon,
});
Category.fromMap(
Map map,
) :
this.id = map['id'],
this.title = map['title'],
this.icon = map['icon'],
Map toMap() {
return {
'id': this.id,
'title': this.title,
'icon': this.icon,
};
}
}
在我的主脚本中我有
List<ToDo> _userToDo = List<ToDO>();
SharedPreferences sharedPreferences;
@override
void initState() {
initSharedPreferences();
super.initState();
}
initSharedPreferences() async {
sharedPreferences = await SharedPreferences.getInstance();
loadDataTodo();
}
void saveDataTodo() {
List<String> spList = _userTodo
.map((todo) => json.encode(todo.toMap()))
.toList();
sharedPreferences.setStringList(todo, spList);
}
void loadDataTodo() {
List<String> spList = sharedPreferences.getStringList(todo);
_userTodo = spList
.map((todo) => todo.fromMap(json.decode(todo)))
.toList();
setState(() {});
}
请帮助我 - 我是 flutter 新手
IconData
需要
int codePoint;
String fontFamily;
String fontPackage;
bool matchTextDirection;
您可以将它们保存到您的存储中并使用它们来(重新)创建您的 IconData。
像这样:
Category.fromMap(
Map map,
) :
this.id = map['id'],
this.title = map['title'],
this.icon = IconData(icon: map['codePoint'],
fontFamily: map['fontFamily'],
fontPackage: map['fontPackage'],
matchTextDirection: map['matchTextDirection'],
),
),
Map toMap() {
return {
'id': this.id,
'title': this.title,
'codePoint': this.icon.codePoint,
'fontFamily': this.icon.fontFamily,
'fontPackage': this.icon.fontPackage,
'matchTextDirection': this.icon.matchTextDirection,
};
}
我建议你使用 localstorage pkg by flutter 而不是使用 sharedPreferences 作为图标数据。
我仍在努力将 iconData 保存到我的待办应用程序的设备存储中,我收到此错误:
Converting object to an encodable object failed: Instance of 'IconData'
在调试控制台中添加待办事项。
如果我取出 iconData,待办事项项会正确保存,当我将其放回原处时,会出现该错误。
import 'package:flutter/foundation.dart';
class ToDo{
final String id;
final String title;
final IconData icon;
const ToDo({
@required this.id,
@required this.title,
@required this.icon,
});
Category.fromMap(
Map map,
) :
this.id = map['id'],
this.title = map['title'],
this.icon = map['icon'],
Map toMap() {
return {
'id': this.id,
'title': this.title,
'icon': this.icon,
};
}
}
在我的主脚本中我有
List<ToDo> _userToDo = List<ToDO>();
SharedPreferences sharedPreferences;
@override
void initState() {
initSharedPreferences();
super.initState();
}
initSharedPreferences() async {
sharedPreferences = await SharedPreferences.getInstance();
loadDataTodo();
}
void saveDataTodo() {
List<String> spList = _userTodo
.map((todo) => json.encode(todo.toMap()))
.toList();
sharedPreferences.setStringList(todo, spList);
}
void loadDataTodo() {
List<String> spList = sharedPreferences.getStringList(todo);
_userTodo = spList
.map((todo) => todo.fromMap(json.decode(todo)))
.toList();
setState(() {});
}
请帮助我 - 我是 flutter 新手
IconData
需要
int codePoint;
String fontFamily;
String fontPackage;
bool matchTextDirection;
您可以将它们保存到您的存储中并使用它们来(重新)创建您的 IconData。
像这样:
Category.fromMap(
Map map,
) :
this.id = map['id'],
this.title = map['title'],
this.icon = IconData(icon: map['codePoint'],
fontFamily: map['fontFamily'],
fontPackage: map['fontPackage'],
matchTextDirection: map['matchTextDirection'],
),
),
Map toMap() {
return {
'id': this.id,
'title': this.title,
'codePoint': this.icon.codePoint,
'fontFamily': this.icon.fontFamily,
'fontPackage': this.icon.fontPackage,
'matchTextDirection': this.icon.matchTextDirection,
};
}
我建议你使用 localstorage pkg by flutter 而不是使用 sharedPreferences 作为图标数据。