更新:感谢@Rjulcaa回答我的地图现在是一个列表,但是该列表未显示在UI上。我转而说我需要使用FutureBuilder处理未来,因此如果有人遇到此问题,请记住将FutureBuilder添加到UI主体,并在构建器上返回要显示的小部件。
我正在制作待办事项列表教程,我正在尝试在todoList上添加JSON编码功能,然后将其保存在sharedPreferences中,并在我启动应用程序时在屏幕上检索它
这是我的TODO课
class Todo {
Todo ({this.title,this.isDone = false});
String title;
bool isDone;
//Decode method to convert a Json String into a Dynamic object
Todo.fromJson(Map <String, dynamic> json)
: title = json ["title"],
isDone = json ["isDone"];
Map <String,dynamic> toJson() =>
{
"title" : title,
"isDone" : isDone
};
}
这是我的屏幕
class _TodoListScreenState extends State<TodoListScreen> {
List<Todo> todos = [];
@override
void initState() {
super.initState();
_getTodoFromSharedPreferences();
}
//updates the state of the checkbox and reflects it on the UI
_toggleTodo(Todo todo, bool isChecked) {
setState(() {
todo.isDone = isChecked;
});
}
_addTodo() async {
final todo = await showDialog<Todo>(
context: context,
builder:(BuildContext context) { // <- Here you draw the Dialog
return NewTodoDialog();
},
);
if (todo != null) {
setState(() {
todos.add(todo);
_saveTodo(todos);
print(todos.length);
});
}
}
_saveTodo(List<Todo> todo) async{
final String newTodo = jsonEncode(todo);
setSharedPreferences(newTodo);
}
_getTodoFromSharedPreferences () async {
final prefs = await SharedPreferences.getInstance();
final savedTodo = prefs.getString("savedTodo");
if (savedTodo == null) {return null;} else {
Map MapofTodos = jsonDecode(savedTodo);
for (var i = 0; i < MapofTodos.length; ++i) {
var o = MapofTodos[i];
setState(() {
Todo todo = Todo.fromJson(o);
todos.add(todo);
});
}
}
}
setSharedPreferences (String newTodo ) async{
final prefs = await SharedPreferences.getInstance();
await prefs.setString("savedTodo", newTodo );
print(newTodo);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(backgroundColor:Colors.deepPurple[900] ,
title: Text('Todo List')),
body: TodoList(
todos: todos,
onTodoToggle: _toggleTodo,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.purpleAccent[700],
child: Icon(Icons.add),
onPressed: _addTodo,
),
);
}
}
当我运行应用程序时,出现此错误
E/flutter ( 7476): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)]
Unhandled Exception: type 'List<dynamic>' is not a subtype of type
'Map<dynamic, dynamic>'
E/flutter ( 7476): #0
_TodoListScreenState._getTodoFromSharedPreferences
(package:flutter_todo/UI/todo_list_screen.dart:70:11)
E/flutter ( 7476): <asynchronous suspension>
E/flutter ( 7476): #1 _TodoListScreenState.initState
(package:flutter_todo/UI/todo_list_screen.dart:29:6)
E/flutter ( 7476): #2 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3851:58)
E/flutter ( 7476): #3 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3716:5)
E/flutter ( 7476): #4 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #5 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #6 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4881:14)
E/flutter ( 7476): #7 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #8 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #9 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3752:16)
E/flutter ( 7476): #10 Element.rebuild
(package:flutter/src/widgets/framework.dart:3564:5)
E/flutter ( 7476): #11 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3721:5)
E/flutter ( 7476): #12 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3716:5)
E/flutter ( 7476): #13 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #14 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #15 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4881:14)
E/flutter ( 7476): #16 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #17 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #18 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4881:14)
E/flutter ( 7476): #19 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #20 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #21 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4881:14)
E/flutter ( 7476): #22 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #23 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #24 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:4881:14)
E/flutter ( 7476): #25 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #26 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #27 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3752:16)
E/flutter ( 7476): #28 Element.rebuild
(package:flutter/src/widgets/framework.dart:3564:5)
E/flutter ( 7476): #29 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3721:5)
E/flutter ( 7476): #30 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3869:11)
E/flutter ( 7476): #31 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3716:5)
E/flutter ( 7476): #32 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #33 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #34 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3752:16)
E/flutter ( 7476): #35 Element.rebuild
(package:flutter/src/widgets/framework.dart:3564:5)
E/flutter ( 7476): #36 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3721:5)
E/flutter ( 7476): #37 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3716:5)
E/flutter ( 7476): #38 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:2960:14)
E/flutter ( 7476): #39 Element.updateChild
(package:flutter/src/widgets/framework.dart:2763:12)
E/flutter ( 7476): #40 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3752:16)
E/flutter ( 7476): #41 Element.rebuild
(package:flutter/src/widgets/framework.dart:3564:5)
E/flutter ( 7476): #42 ComponentElement._firstBuild
(package:flutter/src/widgets/framework.dart:3721:5)
E/flutter ( 7476): #43 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:3869:11)
E/flutter ( 7476): #44 ComponentElement.mount
(package:flutter/src/widgets/framework.dart:3716:5)
E/flutter ( 7476): #45 Ele
我试图将列表更改为地图,但未成功
除了将小部件返回屏幕之外,我在所有事情上都成功完成了。 请帮帮我:c
答案 0 :(得分:0)
我之前遇到过这个问题,因为我想将数组对象保存在共享首选项中。你可以试试看。
data have;
input Pt_ID interval_round DX $10.;
datalines;
4 1 HIV
4 1 Warts
3 1 HIV
5 2 Chlamydia
;
run;
proc sort data=have1; by Pt_Id; run;
proc transpose data=have1 out=want(drop=_NAME_);
by Pt_Id;
id Dx;
var interval_round;
run;
proc print data=want; run;
将JSON转换为字符串数组
//Save you array object as an array of Strings in Shared Preferences
void _saveTodo(List<Todo> todo) async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setStringList("savedTodo", _mapTodoData(todo));
}
}
如果要获取和转换Todo对象列表中的数据
List<String> _mapTodoData(List<dynamic> todos) async{
try {
var res = todos.map((v) => json.encode(v)).toList();
return res;
} catch (err) {
// Just in case
return [];
}
}
}
这对我有用,我真的为这个问题苦苦挣扎。
我希望这对您也有用。
答案 1 :(得分:0)
这里您要遍历Map,这是不可能的,因为只有可能的Iterables对象是Set,List和Queue。
// Map MapofTodos = jsonDecode(savedTodo);
//Try to debug it first first and see what type it's returning
print(jsonDecode(savedTodo).runtimeType);
答案 2 :(得分:0)
在dart应用开发过程中,我遇到了同样的问题。 json解码技术的问题,或者可能是json转换。 我已经完成了将参数传递为
的方法的创建工作popup.showAtLocation(view, Gravity.TOP | Gravity.LEFT, p.right, p.top + 15);
!pip install --target=$'/content/gdrive/My Drive/PythonLibs/' --upgrade pycuda
!pip install --target=$'/content/gdrive/My Drive/PythonLibs/' --upgrade pydicom