목록 항목을 스 와이프하면 데이터베이스에서 해당 항목을 삭제하는 기능을 구현하고 싶습니다. 스 와이프 및 삭제 부분에 dismisible을 사용했지만 데이터베이스에서 항목을 삭제하는 방법을 모르겠습니다. 이것은 내 데이터베이스 도우미 파일입니다.
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:wodo/models/task.dart';
class DatabaseHelper {
Future<Database> database() async {
return openDatabase(
join(await getDatabasesPath(), 'todo_database.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE tasks(id INTEGER PRIMARY KEY, name TEXT, date TEXT)");
},
version: 1,
);
}
Future<void> insertTask(Task task) async {
Database _db = await database();
await _db.insert('tasks', task.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<List<Task>> getTasks() async {
Database db = await database();
List<Map<String, dynamic>> taskMap = await db.query('tasks');
return List.generate(taskMap.length, (index) {
return Task(
id: taskMap[index]['id'],
name: taskMap[index]['name'],
date: taskMap[index]['date']);
});
}
}
실제 데이터베이스 측에서 수행 할 수있는 작업은 다음과 같습니다.
Future<int> delete(int id) async {
final db = await database; // replacd with database instance
return await db.delete(
TABLE, // replace with table name
where: "id = ?",
whereArgs: [id], // you need the id
);
}
실제로 화면을 업데이트하려면 다음과 같이해야합니다.
DatabaseProviderLog.db.delete(log.id).then((_){ // call this action
BlocProvider.of<LogBloc>(context).add(
DeleteLog(index), // uses Bloc Provider and an event to update screen
);
}
별도의 파일에있는 이벤트 :
import 'package:FilamentLeft/models/profiles.dart';
abstract class ProfileEvent {}
class UpdateProfile extends ProfileEvent {
Profile newProfile;
int profileIndex;
UpdateProfile(int index, Profile profile) {
newProfile = profile;
profileIndex = index;
}
}
class SetProfiles extends ProfileEvent {
List<Profile> profileList;
SetProfiles(List<Profile> profiles) {
profileList = profiles;
}
}
class AddProfile extends ProfileEvent {
Profile newProfile;
AddProfile(Profile profile) {
newProfile = profile;
}
}
class DeleteProfile extends ProfileEvent {
int profileIndex;
DeleteProfile(int index) {
profileIndex = index;
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다