Flutter HiveDb将项目添加到列表

时间:2020-08-31 12:45:39

标签: json flutter dart nosql hivedb

当前,我被困在这里,我目前的心态无法找到在flive Hive Db中将项目添加到列表并从列表中删除项目以及从hive db中删除项目的解决方案。我想要的是用户将更新数据库中的任何数据,但是其中有一些限制

这是我要实现的目标

{
    "title": "List1",
    "data": [
        {
            "title": " title",
            "details": "details",
            "data": [
                {
                    "title": " title",
                    "details": " details"
                },
                {
                    "title": " title",
                    "details": " details"
                }
            ]
        },
        {
            "title": " title",
            "details": " details",
            "data": [
                {
                    "title": " title",
                    "details": "details"
                },
                {
                    "title": " title",
                    "details": " details"
                }
            ]
        }
    ]
}

数据模型

0

 class TodoList {
      String title;
      List<Todo> todos;
    
      TodoList({this.title, this.todos});
    
          TodoList.fromJson(Map<String, dynamic> json)
              : title = json['title'],
                todos = json['todos'];
        
          Map<String, dynamic> toJson() => {
            'title' : title,
            'todos': todos
          };
        }

l

class Todo {

  Todo({this.details, this.done, this.title});
  String title;
  String details;
  bool done = false;
  List<Task> task;

  Todo.fromJson(Map<String, dynamic>parsedJson)
  :title = parsedJson['title'],
  details = parsedJson['details'],
  done = parsedJson['done'],
 task = parsedJson['task'];

 
 Map<String, dynamic> toJson() => {
   'title': title,
   'details': details,
   'done': done,
   'task': task
 };
}

2

class Task {
  Task({this.details, this.done, this.title});
  String title;
  String details;
  bool done =false;

  Task.fromJson(Map<String, dynamic>parsedJson)
  :title = parsedJson['title'],
  details = parsedJson['details'],
  done = parsedJson['done'];
}

这是我所拥有的,并且无法正常工作。...

final todoData = todos.getAt(0);
                Map todoJson = jsonDecode(todoData);
                final allData = TodoList.fromJson(todoJson);
                print(allData);
                _listTittle = allData.title;
                print(_listTittle);
                List data = allData.todos;
                Todo todo = data == null ? todoIns : data[index];
                bool done = todo.done;
                done = done == null ? false : done;
                return MaterialButton(
                  padding: EdgeInsets.zero,
                  onPressed: () {},
                  child: Container(
                    color: Colors.white,
                    child: ListTile(
                      leading: IconButton(
                          icon: done
                              ? Icon(
                                  Icons.done,
                                  color: Colors.red,
                                )
                              : Icon(
                                  Icons.done,
                                ),
                          onPressed: () {
                            
                            final todoData = Todo(
                                details: todo.details,
                                title: todo.title,
                                done: done ? false : true);
                            data.insert(index, todoData);
                            final listData =
                                TodoList(title: allData.title, todos: data);
                            updataTodo(listData, index);
                          }),

任何建议都会有所帮助。谢谢

0 个答案:

没有答案