我正在 sqflite 中保存一个配置文件列表,并且我正在尝试使我的列表可重新排序。在我的 Helper 类中,我有以下方法:
static Future insertProfile(Map<String, dynamic> profile) async {
await db.insert('Profiles', profile);
}
static Future deleteProfile(int id) async {
await db.delete('Profiles', where: 'id = ?', whereArgs: [id]);
}
static Future updateProfile(Map<String, dynamic> profile) async {
await db.update('Profiles', profile,
where: 'id = ?', whereArgs: [profile['id']]);
}
我的个人资料列表如下所示:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Profiles')),
body: FutureBuilder(
future: ProfileProvider.getProfileList(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
var profiles = snapshot.data;
return snapshot.data == null
? Container()
: ReorderableListView(
children: List.generate(
profiles.length,
(index) {
return Dismissible(
key: ValueKey("value$index"),
background: slideBackground(),
onDismissed: (direction) {
ProfileProvider.deleteProfile(
profiles[index]['id']);
showSnackBar(
context, profiles[index]['title'], [index]);
},
我正在尝试复制这种类型的函数,但是使用 Sqlite:
void _onReorder(int oldIndex, int newIndex) {
setState(
() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final String item = profiles.removeAt(oldIndex);
profiles.insert(newIndex, item);
},
);
}
到目前为止,我已经成功删除:
onReorder: (int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
}
setState(
() {
ProfileProvider.deleteProfile(profiles[oldIndex]['id']);
},
);
},
问题是我找不到如何在新索引处重新插入相同的配置文件。
我尝试了很多东西,但最新的是:
var item = profiles[oldId]['id'];
ProfileProvider.deleteProfile(profiles[oldId]['id']);
ProfileProvider.insertProfile(item);
但是,我收到此错误消息:
type 'int' is not a subtype of type 'Map<String, dynamic>'
有任何想法吗?