I am new Flutter so sorry for my mistakes from now. I want to delete list item which i chose but i can't. I initialize db this way in main dart :
Database db;
Future createDatabase() async {
db = await openDatabase(
"listoftasks.db",
version: 1,
onCreate: (Database db, int version) {
db.execute(
"CREATE TABLE Tasks (id INTEGER PRIMARY KEY, name TEXT)");
print("creating");
},
onOpen: (Database db) {
print("opened existing database");
},
);
}
@override
void initState() {
createDatabase();
super.initState();
}
and i pulled to MainPage this way :
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: const Color(0xfff9fcff),
body: db == null ? Container() : MainPage(db),
);
}
}
and the MainPage is here ,i described getting database and rendering to page.
List<Task> existingTasks = [];
getTasks() async {
existingTasks = [];
List<Map> tasksQuery =
await widget.db.rawQuery(
"SELECT * FROM Tasks");
for (var i in tasksQuery) {
String name = i["name"];
id = i["id"];
Task newTask = Task(id: id, title: name);
setState(() {
existingTasks.add(newTask);
});
}
}
@override
void initState() {
getTasks();
super.initState();
}
@override
Widget build(BuildContext context) {
return Material(
color: Color(0xffF9FCFF),
child:
TaskPage(
tasks: existingTasks,
db: widget.db,
getTasks: getTasks,
id: id);
My problem is here , I pull db and id for deleting on TaskPage but I have an error. ID is integer and I wrote integer value in sql codes for deleting but the error is being shown. I don't understand why is it. I am sorry for this. Please help me . Delete button and TaskPage :
IconButton(
color: Colors.red,
iconSize: 25,
icon: Icon(Icons.delete),
onPressed: () {
widget.db.rawDelete(
"DELETE FROM Tasks WHERE id = $widget.id ");
//i am pulling id as selected value from MainPage but It doesn't work.
print(widget.id);
widget.getTasks();
}),
The error is :
E/SQLiteLog( 3683): (1) no such column: TaskPage.id E/flutter ( 3683): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: DatabaseException(no such column: TaskPage.id (code 1): , while compiling: DELETE FROM gorevler WHERE id = TaskPage.id) sql 'DELETE FROM gorevler WHERE id = TaskPage.id ' args []}
String interpolation in Flutter is achieved by using ${expression}
. If the expression is an identifier, you can skip the {}
. In your case the expression is not an identifier, so you have to use curly braces. If you are confused about this just make sure to use curly braces for all cases.
This is how you should be doing things:
"DELETE FROM Tasks WHERE id = ${widget.id}"
Your faulty code is basically doing this:
"DELETE FROM Tasks WHERE id = ${widget}.id"