未处理的异常:空值检查运算符用于空值 E/flutter (28057):#0 DBHelper._createDB
Unhandled Exception: Null check operator used on a null value E/flutter (28057): #0 DBHelper._createDB
我为数据库操作创建了一个class,当我触发插入数据库打印的功能时出现这个错误:未处理的异常:空检查运算符用于空值 E/flutter (28057): #0 DBHelper._createDB,一旦初始化 DBHelper
的对象,也不会出现打印。我认为 null-able _db 对象是原因。解决方案是什么?
数据库class:
class DBHelper {
DBHelper._();
factory DBHelper() => instance;
static final DBHelper instance = DBHelper._();
Database? _db;
Future<Database> _createDB() async {
// If the app is opened just now
if (_db == null) return _db!;
// Open db too create a db
// or to take an instance of it
// Get the default databases location.
// and join the name of app db
String path = join(await getDatabasesPath(), 'todo.db');
return _db = await openDatabase(path, version: 1,
onCreate: (Database database, int version) {
database.execute(
"CREATE TABLE tasks(id integer autoincrement PRIMARY KEY,content varchar(150),status varchar(9),isFinished BOOLEAN) ");
print("\n TABLE executed \n");
}, onOpen: (_) {
print("\n opened \n ");
});
}
Future<int> insertNoteToDB(NoteDatabaseModel noteDatabaseModel) async {
Database _database = await _createDB();
return await _database.rawInsert(
'INSERT INTO tasks(content,status) values("${noteDatabaseModel.content}", "today")');
// return await _database!.insert('tasks', noteDatabaseModel.toJason());
}
}
执行sql:
class TodayPage extends StatelessWidget {
const TodayPage();
@override
Widget build(BuildContext context) {
DBHelper dbHelper=DBHelper();
return MaterialButton(
onPressed: () async{
// DBHelper _db = DBHelper.instance;
NoteDatabaseModel note=NoteDatabaseModel(content: "hey",status: "today");
int x= await dbHelper.
insertNodeToDB(note);
print("\n print all Data ${ await dbHelper.
insertNodeToDB(note)}\n");
},
child: const Text("Click Me"),
),
),
],
);
}
}
你认为这行是做什么的:
if (_db == null) return _db!;
如果非要我猜的话,我会说应该是:
if (_db != null) return _db;
我为数据库操作创建了一个class,当我触发插入数据库打印的功能时出现这个错误:未处理的异常:空检查运算符用于空值 E/flutter (28057): #0 DBHelper._createDB,一旦初始化 DBHelper
的对象,也不会出现打印。我认为 null-able _db 对象是原因。解决方案是什么?
数据库class:
class DBHelper {
DBHelper._();
factory DBHelper() => instance;
static final DBHelper instance = DBHelper._();
Database? _db;
Future<Database> _createDB() async {
// If the app is opened just now
if (_db == null) return _db!;
// Open db too create a db
// or to take an instance of it
// Get the default databases location.
// and join the name of app db
String path = join(await getDatabasesPath(), 'todo.db');
return _db = await openDatabase(path, version: 1,
onCreate: (Database database, int version) {
database.execute(
"CREATE TABLE tasks(id integer autoincrement PRIMARY KEY,content varchar(150),status varchar(9),isFinished BOOLEAN) ");
print("\n TABLE executed \n");
}, onOpen: (_) {
print("\n opened \n ");
});
}
Future<int> insertNoteToDB(NoteDatabaseModel noteDatabaseModel) async {
Database _database = await _createDB();
return await _database.rawInsert(
'INSERT INTO tasks(content,status) values("${noteDatabaseModel.content}", "today")');
// return await _database!.insert('tasks', noteDatabaseModel.toJason());
}
}
执行sql:
class TodayPage extends StatelessWidget {
const TodayPage();
@override
Widget build(BuildContext context) {
DBHelper dbHelper=DBHelper();
return MaterialButton(
onPressed: () async{
// DBHelper _db = DBHelper.instance;
NoteDatabaseModel note=NoteDatabaseModel(content: "hey",status: "today");
int x= await dbHelper.
insertNodeToDB(note);
print("\n print all Data ${ await dbHelper.
insertNodeToDB(note)}\n");
},
child: const Text("Click Me"),
),
),
],
);
}
}
你认为这行是做什么的:
if (_db == null) return _db!;
如果非要我猜的话,我会说应该是:
if (_db != null) return _db;