Search code examples
androidfluttersqflite

Flutter Sqflite map object shows setter was called on null


So I am trying to insert a Map object of a Note with values such as title, description, date etc. But it is giving me error Setter was called on null. The map object is showing errors and I am not able to insert values to my sqflite database due to this setter was called on null error. Thanks.

Here is my Code -

save function =>

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

Database helper =>

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

Map =>

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

Error log -

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

Solution

  • I found the solution actually, I forgot to instantiate the Note Object as

    Note note = Note();
    

    That's why it was giving me the null setter error.