【问题标题】:Flutter Sqflite map object shows setter was called on nullFlutter Sqflite 地图对象显示在 null 上调用了 setter
【发布时间】:2026-01-04 04:10:02
【问题描述】:

所以我试图插入带有标题、描述、日期等值的 Note 的 Map 对象。 但它给了我错误 Setter was called on null。地图对象显示错误,我无法将值插入我的 sqflite 数据库,因为此设置器在空错误时被调用。 谢谢。

这是我的代码 -

保存功能 =>

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

【问题讨论】:

    标签: android flutter sqflite


    【解决方案1】:
    note.priority = 1;
    

    这是您在Note 类中创建的getter。您将其用作二传手。

    你可以做这两件事之一,

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

    【讨论】:

    • 不能使用 note._priority = 1 因为 _priority 是类 Note 的私有字段。
    • 然后创建一个自定义设置器。 (第二个选项)
    【解决方案2】:

    其实我找到了解决方案,我忘记将 Note 对象实例化为

    Note note = Note();
    

    这就是它给我空设置器错误的原因。

    【讨论】:

      最近更新 更多