Open syifakamin opened 3 years ago
You should be able to get the stack trace to understand better where the issue happen. In you code we don't see any call to getTaskList so this likely happening somewhere else. My guess is that somewhere you might be calling DatabaseHelper.instance.getTaskList()
and that the instance is null (the constructor is private but I don't see where the instance is created). You should also be able to debug to watch the various variable values.
` import 'dart:async'; import 'dart:core'; import 'dart:io';
import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; import 'package:todolist/models/task_model.dart';
class DatabaseHelper { static final DatabaseHelper instance = DatabaseHelper.instance; static Database _db;
DatabaseHelper._instance();
String tasksTable = 'task_table'; String colId = 'id'; String colTitle = 'title'; String colDate = 'date'; String colPriority = 'priority'; String colStatus = 'status';
Future get db async{
if (_db == null) {
_db = await _initDb();
}
return _db;
}
Future _initDb() async{
Directory dir = await getApplicationDocumentsDirectory();
String path = dir.path + 'todo_list.db';
final todoListdb =
await openDatabase(path, version: 1, onCreate: _createDb);
return todoListdb;
}
void _createDb(Database db, int version) async{ await db.execute( "CREATE TABLE $tasksTable($colId INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, $colDate TEXT, $colPriority TEXT, $colStatus INTEGER" ); }
Future<List<Map<String, dynamic>>> getTaskMapList() async{ Database db = await this.db; final List<Map<String, dynamic>> result = await db.query(tasksTable); return result; }
Future<List> getTaskList() async{
final List<Map<String, dynamic>> taskMapList = await getTaskMapList();
final List taskList = [];
taskMapList.forEach((taskMap) {
taskList.add(Task.fromMap(taskMap));
});
return taskList;
}
Future insertTask(Task task) async {
Database db = await this.db;
final int result = await db.insert(tasksTable, task.toMap());
return result;
}
Future updateTask(Task task) async{
Database db = await this.db;
final int result = await db.update(
tasksTable,
task.toMap(),
where: '$colId = ?',
whereArgs: [task.id],
);
return result;
}
Future deleteTask(int id)async{
Database db = await this.db;
final int result = await db.delete(
tasksTable,
where: '$colId = ?',
whereArgs: [id],
);
return result;
}
}`
So here's my code, when i run the application the error The method 'getTaskList' was called on null. Receiver: null Tried calling: getTaskList(). Would be good if anyone could help.