Json 如何在本地保存列表数据

Json 如何在本地保存列表数据,json,database,file,flutter,dart,Json,Database,File,Flutter,Dart,我正在构建一个待办事项列表应用程序,我希望将数据存储在本地,以便每次打开该应用程序时,我都能获得以前创建的所有任务。我是新手,这是我的第一个应用程序。我已经尝试将数据保存到一个文件中,创建一个JSON文件来保存数据,并尝试使用数据库。似乎什么都不管用。有人能帮我吗 这是我的代码:- import 'package:flutter/material.dart'; class toDoList extends StatefulWidget { bool data = false;

我正在构建一个待办事项列表应用程序,我希望将数据存储在本地,以便每次打开该应用程序时,我都能获得以前创建的所有任务。我是新手,这是我的第一个应用程序。我已经尝试将数据保存到一个文件中,创建一个JSON文件来保存数据,并尝试使用数据库。似乎什么都不管用。有人能帮我吗

这是我的代码:-

import 'package:flutter/material.dart';

class toDoList extends StatefulWidget
{
    bool data = false;
    @override
    createState() 
    {
        return new toDoListState();
    }
}

class toDoListState extends State<toDoList>
{
  List<String> tasks = [];
  List<bool> completedTasks = [];
  List<String> descriptions = [];
  List<bool> importance = [];


    @override
    Widget build(BuildContext context)
    {
        return new Scaffold
        (
            body: buildToDoList(),
            floatingActionButton: new FloatingActionButton
            (
                onPressed: addToDoItemScreen, 
                tooltip: 'Add Task',
                child: new Icon(Icons.add),
            ),
        );
    }

    Widget buildToDoList()
    {
        return new ListView.builder
        (
            itemBuilder: (context, index)
            {
                if(index < tasks.length)
                {
                  if(tasks[index] == "#45jiodg{}}{OHU&IEB")
                  {
                    tasks.removeAt(index);
                    descriptions.removeAt(index);
                    importance.removeAt(index);
                  } 
                    return row(tasks[index], descriptions[index], index);
                };
            },
        );
    }

    Widget row(String task, String description, int index)
    {                  
        return Dismissible(
        key: UniqueKey(),
        background: Container(color: Colors.red, child: Align(alignment: Alignment.center, child: Text('DELETE', textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 18),))),
        direction: DismissDirection.horizontal,
        onDismissed: (direction) {
        setState(() {
          tasks.removeAt(index);
          if(completedTasks[index])
          {
              completedTasks.removeAt(index);
          }
          descriptions.removeAt(index);
          importance.removeAt(index);
        });
          Scaffold.of(context).showSnackBar(SnackBar(content: Text(task+" dismissed")));
        },
        child: CheckboxListTile(
          controlAffinity: ListTileControlAffinity.leading,
          title: Text(task, style: (completedTasks[index]) ? TextStyle(decoration: TextDecoration.lineThrough) : TextStyle(),),
          subtitle: Text(descriptions[index], style: (completedTasks[index]) ? TextStyle(decoration: TextDecoration.lineThrough) : TextStyle(),),
          isThreeLine: true,
          secondary: (importance[index])? Icon(Icons.error, color: Colors.red,) : Text(''),
          value: completedTasks[index],
          onChanged: (bool value) {
           setState(() {
              if(completedTasks[index])
              {
                  completedTasks[index] = false;
              }
              else
              {
                  completedTasks[index] = true;
              }
           });
          },
        ));
    }
  
  void addToDoItemScreen() {
    int index = tasks.length;
    while (importance.length > tasks.length) {
      importance.removeLast();
    }
    importance.add(false);
    tasks.add("#45jiodg{}}{OHU&IEB");
    descriptions.add("No Description");
    completedTasks.add(false);
    
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return StatefulBuilder(builder: (context, setState) { // this is new
                return new Scaffold(
                    appBar: new AppBar(title: new Text('Add a new task')),
                    body: Form(
                      child: Column(
                        children: <Widget>[
                          TextField(
                            autofocus: true,
                            onSubmitted: (name) {
                              addToDoItem(name);
                              //Navigator.pop(context); // Close the add todo screen
                            },
                            decoration: new InputDecoration(
                                hintText: 'Enter something to do...',
                                contentPadding: const EdgeInsets.all(20.0),
                                border: OutlineInputBorder()),
                          ),
                          TextField(
                            //autofocus: true,
                            //enabled: descriptions.length > desc,
                            onSubmitted: (val) {
                              addDescription(val, index);
                            },
                            decoration: new InputDecoration(
                                hintText: 'Enter a task decription...',
                                contentPadding: const EdgeInsets.all(20.0),
                                border: OutlineInputBorder()),
                          ),
                          Row(
                            children: <Widget> [
                              Switch(
                              value: importance[index],
                              onChanged: (val) {
                                setState(() {
                                });
                                impTask(index);
                              },
                            ),
                            Text('Important Task', style: TextStyle(fontSize: 18)),
                            ],
                          ),
                  RaisedButton(onPressed: () { Navigator.pop(context); }, child: Text('DONE', style: TextStyle(fontSize: 20)),)
                ],
              ),
            ));
      });
    }));
  }

    void addToDoItem(String task)
    {
        setState(() {
          tasks.last = task;
        });
    }

    void addDescription(String desc, int index)
    {
        setState(() {
          descriptions.last = desc;
        });
    }

    void impTask(int index)
    {
        setState(() {
          if(importance[index])
          {
            importance[index] = false;
          }
          else 
          {
            importance[index] = true;
          }
        });
    }

    
}
导入“包装:颤振/材料.省道”;
类toDoList扩展了StatefulWidget
{
布尔数据=假;
@凌驾
createState()
{
返回新的toDoListState();
}
}
类toDoListState扩展了状态
{
列出任务=[];
列出已完成的任务=[];
列表描述=[];
列表重要性=[];
@凌驾
小部件构建(构建上下文)
{
归还新脚手架
(
正文:buildToDoList(),
floatingActionButton:新的floatingActionButton
(
onPressed:addToDoItemScreen,
工具提示:“添加任务”,
子:新图标(Icons.add),
),
);
}
小部件buildToDoList()
{
返回新的ListView.builder
(
itemBuilder:(上下文,索引)
{
如果(索引tasks.length){
重要性。removeLast();
}
重要性。添加(false);
添加(“#45jiodg{}{OHU&IEB”);
说明。添加(“无说明”);
completedTasks.add(false);
Navigator.of(context.push(newmaterialpageroute)(生成器:(context){
return StatefulBuilder(builder:(context,setState){//这是新的
归还新脚手架(
appBar:new appBar(标题:新建文本(“添加新任务”),
正文:表格(
子:列(
儿童:[
文本字段(
自动对焦:对,
提交人:(姓名){
addToDoItem(名称);
//Navigator.pop(context);//关闭addtodo屏幕
},
装饰:新的输入装饰(
hintText:“输入要做的事情…”,
contentPadding:const EdgeInsets.all(20.0),
边框:OutlineInputBorder()),
),
文本字段(
//自动对焦:对,
//已启用:descriptions.length>desc,
提交:(val){
addDescription(val,index);
},
装饰:新的输入装饰(
hintText:'输入任务说明…',
contentPadding:const EdgeInsets.all(20.0),
边框:OutlineInputBorder()),
),
划船(
儿童:[
开关(
值:重要性[索引],
一旦更改:(val){
设置状态(){
});
输入任务(索引);
},
),
Text(“重要任务”,样式:TextStyle(fontSize:18)),
],
),
RaisedButton(按下时:(){Navigator.pop(context);},子项:Text('DONE',style:TextStyle(fontSize:20)),)
],
),
));
});
}));
}
void addToDoItem(字符串任务)
{
设置状态(){
tasks.last=ta