Flutter 将图标保存到Flatter中的共享首选项

Flutter 将图标保存到Flatter中的共享首选项,flutter,dart,Flutter,Dart,我仍在为我的待办应用程序将IConda保存到设备存储中而努力,我遇到以下错误: 将对象转换为可编码对象失败:“IconData”的实例 在调试控制台中添加待办事项时 如果我取出iconData,待办事项将正确保存,当我将其放回时,会出现该错误 import 'package:flutter/foundation.dart'; class ToDo{ final String id; final String title; final IconData icon;

我仍在为我的待办应用程序将IConda保存到设备存储中而努力,我遇到以下错误:

将对象转换为可编码对象失败:“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(() {});
  }
List\u userToDo=List();
SharedReferences SharedReferences;
@凌驾
void initState(){
initSharedReferences();
super.initState();
}
initSharedReferences()异步{
SharedReferences=等待SharedReferences.getInstance();
loadDataTodo();
}
void saveDataTodo(){
List spList=\u userTodo
.map((todo)=>json.encode(todo.toMap())
.toList();
sharedPreferences.setStringList(todo,spList);
}
void loadDataTodo(){
List spList=SharedReferences.getStringList(todo);
_userTodo=spList
.map((todo)=>todo.fromMap(json.decode(todo)))
.toList();
setState((){});
}

请帮帮我-我不熟悉颤振

在loadDataTodo中设置状态,除非您这样做,否则它不会有多大用处:

void loadDataTodo() {
    List<String> spList = sharedPreferences.getStringList(todo);
    setState(() {
       _userTodo = spList
        .map((todo) => todo.fromMap(json.decode(todo)))
        .toList();
    });
  }

除非执行以下操作,否则loadDataTodo中的setState不会有太大作用:

void loadDataTodo() {
    List<String> spList = sharedPreferences.getStringList(todo);
    setState(() {
       _userTodo = spList
        .map((todo) => todo.fromMap(json.decode(todo)))
        .toList();
    });
  }

请更具体地说,哪一行代码返回错误?最有可能的情况是IconData无法序列化为适合存储的格式请更具体地说,哪一行代码返回错误?很可能IConda无法序列化为适合存储的格式