简体   繁体   English

如何解决“必须初始化不可为空的变量'_db'。尝试添加初始化表达式”

[英]How to fix "The non-nullable variable '_db' must be initialized. Try adding an initializer expression"

"The non-nullable variable '_db' must be initialized. Try adding an initializer expression" Could anyone help me with this non-nullable error? “必须初始化不可为空的变量'_db'。尝试添加初始化表达式”有人可以帮我解决这个不可为空的错误吗? I am using flutter to make an android app and while using the 'sqflite' and making a database, this error pops up I don't know how to really fix this Here is my Code below, I am using the last version of Flutter & Dart ,我正在使用 flutter 制作 android 应用程序,在使用“sqflite”并制作数据库时,弹出此错误我不知道如何真正解决此问题 这是我的代码下面,我使用的是ZC047B10EEE763AFB6164E07的最新版本Dart ,

import 'dart:io';
import 'package:newtodoapp/taskmodel.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

class DatabaseHelper {
  static final DatabaseHelper instance = DatabaseHelper._instance();
  static Database _db;


  DatabaseHelper._instance();


  String tasksTable = 'task_table';
  String colId = 'id';
  String colTitle = 'title';
  String colDate = 'date';
  String colPriority = 'priority';
  String colStatus = 'status';

  // Task Tables
  // Id | Title | Date | Priority | Status
  // 0     ''      ''      ''         0
  // 2     ''      ''      ''         0
  // 3     ''      ''      ''         0

  Future<Database> get db async {
    if (_db == null) {
    _db = await _initDb();
   }
   return _db;
  }
  Future<Database> _initDb() async {
    Directory dir = await getApplicationDocumentsDirectory();
    String path = dir.path + '/todo_list.db';
    final todoListDb =
    await openDatabase(path, version: 1, onCreate: _createDb);
    return todoListDb;
  }

  void _createDb(Database db, int version) async {
   await db.execute(
  'CREATE TABLE $tasksTable($colId INTEGER PRIMARY KEY AUTOINCREMENT,'
      ' $colTitle TEXT, $colDate TEXT, $colPriority TEXT, $colStatus INTEGER)',
    );
  }

  Future<List<Map<String, dynamic>>> getTaskMapList() async {
    Database db = await this.db;
    final List<Map<String, dynamic>> result = await db.query(tasksTable);
   return result;
  }

  Future<List<Task>> getTaskList() async {
   final List<Map<String, dynamic>> taskMapList = await getTaskMapList();
   final List<Task> taskList = [];
   taskMapList.forEach((taskMap) {
   taskList.add(Task.fromMap(taskMap));
  });
  taskList.sort((taskA, taskB) => taskA.date.compareTo(taskB.date));
  return taskList;
   }

   Future<int> insertTask(Task task) async {
   Database db = await this.db;
   final int result = await db.insert(tasksTable, task.toMap());
   return result;
 }

 Future<int> updateTask(Task task) async {
   Database db = await this.db;
   final int result = await db.update(
   tasksTable,
   task.toMap(),
   where: '$colId = ?',
   whereArgs: [task.id],
   );
   return result;
  }

  Future<int> deleteTask(int id) async {
   Database db = await this.db;
   final int result = await db.delete(
   tasksTable,
   where: '$colId = ?',
   whereArgs: [id],
   );
  return result;
   }
 }

declare your variable like this.像这样声明你的变量。

static Database? static 数据库? _db; _D b;

暂无
暂无

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

相关问题 Flutter SQL flite - 必须初始化不可为 null 的变量“_database”。 尝试添加初始化表达式 - Flutter SQL flite - The non-nullable variable '_database' must be initialized. Try adding an initializer expression 必须初始化不可为空的变量“_database” - The non-nullable variable '_database' must be initialized 不可为空的实例字段 &#39;_database&#39; 必须被初始化 - Non-nullable instance field '_database' must be initialized ExecuteReader:连接属性尚未初始化。 如何解决这个问题? - ExecuteReader: Connection property has not been initialized. How to fix this? Django 1.7:Makemigration:不可空的字段 - Django 1.7: Makemigration: non-nullable field 将不可为空的列添加到SQL Server中的现有表? - Add non-nullable columns to an existing table in SQL server? Hibernate @OneToMany &amp; @OneToOne 关联到不可为空的实体 - Hibernate @OneToMany & @OneToOne associations to non-nullable entity 使用postgresql-rails时,如何使用id列的值作为另一个非空列的默认值? - How can I use value of the id column as default value of another non-nullable column when using postgresql-rails? 实体框架无法更改关系,因为一个或多个外键属性不可为空 - Entity Framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系asdf - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable, asdf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM