gpt4 book ai didi

Flutter Sembast 插入或更新

转载 作者:行者123 更新时间:2023-12-03 02:48:55 24 4
gpt4 key购买 nike

如何在flutter中用sembast库实现insert if no records with the id else update?我试过下面的。

Future updateOrInsert(User user) async {
final result = await update(user);
if(result == 0){
insert(user);
}
}

Future update(User user) async {
final finder = Finder(filter: Filter.byKey(user.id));
final result = await _userStore.update(await _db, user.toMap(), finder: finder);
return result;
}

Future insert(User user) async {
await _userStore.add(await _db, user.toMap());
}

它工作正常。这是正确的解决方案还是有任何其他可用的直接 sembast API?

sembast

最佳答案

您应该使用 Record.put 添加或更新具有给定 ID 的记录。

Future updateOrInsert(User user) async {
await _userStore.record(user.id).put(_db, user.toMap());
}

关于Flutter Sembast 插入或更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59578986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com