Flutter Sqflite 地图 object 显示 setter 被调用为 null

Flutter Sqflite map object shows setter was called on null

所以我试图插入一个注释的地图 object,其中包含标题、描述、日期等值。 但它给我错误 Setter was called on null。地图 object 显示错误,我无法将值插入我的 sqflite 数据库,因为此 setter 被调用为空错误。 谢谢。

这是我的代码 -

保存功能=>

void _saveNote() async {
note.priority = 1;
note.title = titleTextController.text;
note.description = descriptionTextController.text;
note.date = DateFormat.yMMMd().format(DateTime.now());
int result = await databaseHelper.insertNote(note);
if (result != 0) {
  _showSnackBar(context, "Note Saved");
} else {
  _showSnackBar(context, "Error Saving Note");
}
}

数据库助手=>

class DatabaseHelper {
 static DatabaseHelper _databaseHelper;
 static Database _database;

  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;
   }

 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 + 'notes.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;

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 {
var 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 {
var db = await this.database;
int 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 = List<Note>();
for (int i = 0; i < count; i++) {
  noteList.add(Note.fromMapObject(noteMapList[i]));
}

return noteList;
}
}

地图=>

class Note {
int _id;
 String _title;
String _description;
String _date;
int _priority;

Note(this._title, this._date, this._priority, [this._description]);

Note.withId(this._id, this._title, this._date, this._priority,
  [this._description]);

 int get id => _id;

 String get title => _title;

 String get description => _description;

 int get priority => _priority;

 String get date => _date;

 set title(String newTitle) {
 if (newTitle.length <= 255) {
  this._title = newTitle;
 }
 }

 set description(String newDescription) {
 if (newDescription.length <= 255) {
  this._description = newDescription;
 }
 }

 set priority(int newPriority) {
 if (newPriority >= 1 && newPriority <= 2) {
  this._priority = newPriority;
  }
 }

set date(String newDate) {
 this._date = newDate;
}

Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
if (id != null) {
  map['id'] = _id;
}
map['title'] = _title;
map['description'] = _description;
map['priority'] = _priority;
map['date'] = _date;

return map;
}

Note.fromMapObject(Map<String, dynamic> map) {
this._id = map['id'];
this._title = map['title'];
this._description = map['description'];
this._priority = map['priority'];
this._date = map['date'];
}
}

错误日志-

E/SpannableStringBuilder(18034): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
I/SurfaceView(18034): updateWindow -- setFrame, this = 
io.flutter.embedding.android.FlutterSurfaceView{a889cc1 V.E...... ......I. 0,0-720,1344}
6
E/SpannableStringBuilder(18034): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/flutter (18034): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 
NoSuchMethodError: The setter 'priority=' was called on null.
E/flutter (18034): Receiver: null
E/flutter (18034): Tried calling: priority=1
E/flutter (18034): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (18034): #1      _NewNoteState._saveNote
package:NotesApp/views/newNote.dart:25
E/flutter (18034): #2      _NewNoteState.build.<anonymous closure>
package:NotesApp/views/newNote.dart:12
note.priority = 1;

这是您在 Note class 中创建的 getter。您将其用作 setter.

你可以做这两件事之一,

  1. 使用note._priority = 1推荐
  2. 创建自定义 setter 我认为这不是必需的。

其实我找到了解决办法,我忘了实例化Note对象为

Note note = Note();

这就是它给我空 setter 错误的原因。