简体   繁体   English

DB Sqflite Flutter 中的数据未更新

[英]Data not updating in DB Sqflite Flutter

The task text in edit_todo_screen is not updated. edit_todo_screen中的任务文本未更新。 I'm using the same code as in item_tasks , where I change the status of the task to move between "Done" and "Archived" - everything works well here.我使用的代码与item_tasks中的代码相同,在这里我将任务的状态更改为在“完成”和“存档”之间移动——这里一切正常。 I tried to change only the status in edit_todo_screen , but it does not change, although the code is identical to the code in item_tasks .我尝试仅更改edit_todo_screen中的状态,但它没有更改,尽管代码与item_tasks中的代码相同。 Perhaps the problem is that I'm not passing the parameters correctly to edit_todo_screen .也许问题是我没有将参数正确传递给edit_todo_screen I need to be able to change the status of the task and the text of the task itself in edit_todo_screen .我需要能够在edit_todo_screen中更改任务的状态和任务本身的文本。 Attached below is a screenshot of the error that occurs when clicking the button in edit_todo_screen Tell me, please, what could be my mistake?下面附上点击edit_todo_screen中的按钮时出现的错误截图 请告诉我,我的错误可能是什么?

cubit_db cubit_db

 class AppCubit extends Cubit<AppStates> {
      AppCubit() : super(AppInitialState());
    
      static AppCubit get(context) => BlocProvider.of(context);
    
    void updateDatabase(String status, int id) async {
        database!.rawUpdate(
            'UPDATE tasks SET status = ? WHERE id = ?', [status, id]).then((value) {
          getDataBase(database);
          emit(AppUpdateDatabaseState());
        });
      }

new_tasks_list新任务列表

class NewTasksScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<AppCubit, AppStates>(
      listener: (context, state) {},
      builder: (context, state) {
        var tasks = AppCubit.get(context).newTasks;
        return SingleChildScrollView(
          child: Column(children: [
            ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: tasks.length,
              itemBuilder: (context, index) => TaskItem(tasks: tasks[index]),
            ),
          ]),
        );
      },
    );

tasks_item任务项

class TaskItem extends StatelessWidget {
  Map? tasks;

  TaskItem({this.tasks});
  @override
  Widget build(BuildContext context) {
    return Card(
      key: Key(tasks!['title']),
      shadowColor: Colors.blueGrey,
      margin: const EdgeInsets.only(left: 15, right: 15, top: 8),
      color: Colors.black,
      shape: RoundedRectangleBorder(
        side: BorderSide(color: Colors.grey.shade800, width: 0.5),
        borderRadius: BorderRadius.circular(10),
      ),
      borderOnForeground: false,
      child: ListTile(
        title: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Text(
            // '${state.loadedUser[index].description}',
            tasks!['title'],
            style: const TextStyle(
              fontSize: 21.0,
              // fontWeight: FontWeight.bold,
            ),
          ),
          // Text(
          //   tasks!['status'],
          //   style: const TextStyle(fontSize: 21.0),
          // ),
        ]),
        trailing: IconButton(
          tooltip: 'Archive Todo',
          highlightColor: Colors.red,
          onPressed: () {
            AppCubit.get(context).updateDatabase('Archive', tasks!['id']);
          },
          icon: const Icon(
            Icons.archive,
            color: Colors.white,
          ),
        ),
        leading: IconButton(
          tooltip: 'Done Todo',
          highlightColor: Colors.green,
          onPressed: () {
            AppCubit.get(context).updateDatabase('Done', tasks!['id']);
          },
          icon: const Icon(
            Icons.check,
            color: Colors.white,
          ),
        ),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => EditTodoScreen(
                title: tasks!['title'],
                id: tasks!['id'],
              ),
            ),
          );
        },
      ),
    );
  }
}

edit_todo_screen编辑待办事项屏幕

class EditTodoScreen extends StatelessWidget {
  // Map? tasks;
  String title;
  int id;
  EditTodoScreen({Key? key, required this.title, required this.id})
      : super(key: key);

  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _controller.text = title;

    return BlocConsumer<AppCubit, AppStates>(
        listener: (context, state) {},
        builder: (context, state) {
          return Scaffold(
            appBar: AppBar(
              title: const Text(
                'Edit Todo',
                style: TextStyle(fontSize: 20.0),
              ),
            ),
            body: _body(context),
          );
        });
  }

  Widget _body(context) {
    return Padding(
      padding: const EdgeInsets.all(20.0),
      child: Column(
        children: [
          TextFormField(
            controller: _controller,
            autocorrect: true,
            maxLines: 2,
            decoration: const InputDecoration(hintText: 'Enter todo message'),
          ),
          const SizedBox(
            height: 10.0,
          ),
          // ElevatedButton(
          //   // style:,
          //   onPressed: () {
          //     AppCubit.get(context).updateDatabase('Done', id);
          //   },
          //   child: Text(
          //     'Update Data',
          //     style: TextStyle(color: Colors.amber.shade700),
          //   ),
          // ),
          InkWell(
            onTap: () {
              AppCubit.get(context).updateDatabase('Done', id);
              Navigator.pop(context);
            },
            child: _updateBtn(context),
          )
        ],
      ),
    );
  }

  Widget _updateBtn(context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 50.0,
      decoration: BoxDecoration(
          color: Colors.black, borderRadius: BorderRadius.circular(10.0)),
      child: Center(
        child: Text(
          'Update Todo',
          style: TextStyle(
              fontSize: 17.0,
              color: Colors.amber.shade700,
              fontWeight: FontWeight.bold),
        ),
      ),
    );
  }
}

在此处输入图像描述

I think your problem has to do with the fact that database is not set in the second case.我认为您的问题与第二种情况下未设置数据库有关。 The code fails because you try to access a null value that then is checked with the "."代码失败,因为您尝试访问 null 值,然后使用“。”进行检查。 operator.操作员。 Look where you set the database and check if that code is called in both code flows.查看您设置数据库的位置并检查是否在两个代码流中都调用了该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM