延迟初始化错误: 字段 '_databaseHelper@26286205' 尚未初始化



我有Late initialization error。我长期面临这个问题,我尝试了所有可能的方法来解决它,但是,我失败了,有人能帮助吗?

==============================
database_helper.dart
==============================
import 'Note.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static late DatabaseHelper _databaseHelper; //Singleton
static late Database _database; // Singleton
String noteTable = 'note_table';
String colId = 'id';
String colTitle = 'title';
String colDescription = 'description';
String colPriority = 'priority';
String colDate = 'date';
DatabaseHelper._createInstance();
factory DatabaseHelper() {
if (DatabaseHelper == null) {
_databaseHelper = DatabaseHelper._createInstance();
}
return _databaseHelper;
}
// custom getter
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database;
}
Future<Database> initializeDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'note.db';
var notesDatabase = await openDatabase(
path,
version: 1,
onCreate: _createDB,
);
return notesDatabase;
}
void _createDB(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $noteTable($colId INTEGER PRIMARY KEY AUTOINCREMENT,$colTitle TEXT,$colDescription TEXT, $colPriority INTEGER,$colDate TEXT)');
}
Future<List<Map<String, dynamic>>> getNoteMapList() async {
Database db = await this.database;
// regular MySQL query optional
// var result =
//     await db.rawQuery('SELECT * from $noteTable order by $colPriority ASC');
var result = await db.query(noteTable, orderBy: '$colPriority ASC');
return result;
}
Future<int> insertNote(Note note) async {
Database db = await this.database;
var result = await db.insert(noteTable, note.toMap());
return result;
}
Future<int> updateNote(Note note) async {
Database db = await this.database;
var result = await db.update(noteTable, note.toMap(),
where: '$colId=?', whereArgs: [note.id]);
return result;
}
Future<int> deleteNote(int id) async {
Database db = await this.database;
var result = await db.rawDelete('DELETE FROM $noteTable where $colId=$id');
return result;
}
Future<int?> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> x =
await db.rawQuery('SELECT COUNT (*) from $noteTable');
int? result = Sqflite.firstIntValue(x);
return result;
}
Future<List<Note>> getNoteList() async {
var noteMapList = await getNoteMapList();
int count = noteMapList.length;
List<Note> noteList = <Note>[];
for (var i = 0; i < count; i++) {
noteList.add(Note.fromMapObject(noteMapList[i]));
}
return noteList;
}
}

下面是note_detail。Dart,它正在使用数据库助手功能。我已经创建了一些方法和getter这是使用数据库。它也给出了一个错误,而我没有使用"return"在WillPopScope () .

======================
note_detail.dart
======================
import 'package:flutter/material.dart';
import '../Note.dart';
import '../database_helper.dart';
import 'package:intl/intl.dart';
class NoteDetail extends StatefulWidget {
final String appBarTitle;
final Note note;
NoteDetail(this.note, this.appBarTitle);
@override
State<StatefulWidget> createState() {
return NoteDetailState(this.note, this.appBarTitle);
}
}
class NoteDetailState extends State<NoteDetail> {
static var _priorities = ['High', 'Low'];
DatabaseHelper helper = DatabaseHelper();
String appBarTitle;
Note note;
NoteDetailState(this.note, this.appBarTitle);
TextEditingController titleController = TextEditingController();
TextEditingController descriptionController = TextEditingController();
@override
Widget build(BuildContext context) {
TextStyle? textStyle = Theme.of(context).textTheme.titleMedium;
titleController.text = note.title;
descriptionController.text = note.description!;
return WillPopScope(
onWillPop: () async {
bool? result = await movetoLastScreen();
result ??= false;
return result;
},
child: Scaffold(
backgroundColor: Colors.cyanAccent,
appBar: AppBar(
title: Text(appBarTitle),
backgroundColor: Colors.pink,
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
movetoLastScreen();
},
),
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 5.0),
//dropdown menu
child: new ListTile(
leading: const Icon(Icons.low_priority),
title: DropdownButton(
items: _priorities.map((String dropDownStringItem) {
return DropdownMenuItem<String>(
value: dropDownStringItem,
child: Text(dropDownStringItem,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.red)),
);
}).toList(),
value: getPriorityAsString(note.priority),
onChanged: (valueSelectedByUser) {
setState(() {
updatePriorityAsInt(valueSelectedByUser!);
});
}),
),
),
// Second Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0, left: 15.0),
child: TextField(
controller: titleController,
style: textStyle,
onChanged: (value) {
updateTitle();
},
decoration: InputDecoration(
labelText: 'Title',
labelStyle: textStyle,
icon: Icon(Icons.title),
),
),
),
// Third Element
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 15.0, left: 15.0),
child: TextField(
controller: descriptionController,
style: textStyle,
onChanged: (value) {
updateDescription();
},
decoration: InputDecoration(
labelText: 'Details',
icon: Icon(Icons.details),
),
),
),
// Fourth Element
Padding(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Expanded(
child: ElevatedButton(
child: Text(
'Save',
textScaleFactor: 1.5,
style: TextStyle(
color: Colors.white,
),
),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
padding: const EdgeInsets.all(8.0),
),
onPressed: () {
setState(() {
debugPrint("Save button clicked");
_save();
});
},
),
),
Container(
width: 5.0,
),
Expanded(
child: ElevatedButton(
child: Text(
'Delete',
textScaleFactor: 1.5,
style: TextStyle(
color: Colors.white,
),
),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
padding: const EdgeInsets.all(8.0),
),
onPressed: () {
setState(() {
_delete();
});
},
),
),
],
),
),
],
),
),
),
),
);
}
void updateTitle() {
note.title = titleController.text;
}
void updateDescription() {
note.description = descriptionController.text;
}
void _save() async {
movetoLastScreen();
}
void _delete() async {
movetoLastScreen();
if (note.id == null) {
_showAlertDialog('Status', 'First add a note');
return;
}
int? result = await helper.deleteNote(note.id);
if (result != 0) {
_showAlertDialog('Status', 'Note deleted Successfully');
} else {
_showAlertDialog('Status', 'Error');
}
}
// convert to int to save into database
void updatePriorityAsInt(String value) {
switch (value) {
case 'High':
note.priority = 1;
break;
case 'Low':
note.priority = 2;
break;
}
}
// convert int to String to show user
String getPriorityAsString(int value) {
late String priority;
switch (value) {
case 1:
priority = _priorities[0];
break;
case 2:
priority = _priorities[1];
break;
}
return priority;
}
movetoLastScreen() async {
Navigator.pop(context, true);
note.date = DateFormat.yMMMd().format(DateTime.now());
int? result;
if (note.id != null) {
result = await helper.updateNote(note);
} else {
result = await helper.insertNote(note);
}
if (result != 0) {
_showAlertDialog('Status', 'Note Save Successfully');
} else {
_showAlertDialog('Status', 'Problem saving Note');
}
}
void _showAlertDialog(String title, String message) {
AlertDialog alertDialog = AlertDialog(
title: Text(title),
content: Text(message),
);
showDialog(context: context, builder: (_) => alertDialog);
}
}

我已经尝试了所有可能的是否检查Nullasync问题,但没有任何工作。

您需要使数据类型为空来进行空检查。

class DatabaseHelper {
static DatabaseHelper? _databaseHelper;  
static Database? _database;  
....
DatabaseHelper._createInstance();
factory DatabaseHelper() {
if (_databaseHelper == null) {
_databaseHelper = DatabaseHelper._createInstance();
}
return _databaseHelper!;
}
// custom getter
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database;
}

查找有关null-safety的更多信息

static late DatabaseHelper _databaseHelper; //Singleton
static late Database _database; // Singleton

DatabaseHelper _databaseHelper = DatabaseHelper._createInstance();
Database _database ; 

最新更新