我有一个存储 map 对象的Sembast商店。如果要删除 map 中的键,是否必须更新整个 map 对象,或者是否有办法在不更新整个 map 的情况下删除 key ?这些文档仅显示如何删除记录,而没有显示如何删除字段。由于存在一种更新单个字段的方法,因此我认为将此功能用于其他操作是有意义的。
例:
// store a map that contains another map
int key = await petStore.add(db, {'name': 'fish', friends : {'cats' : 0, 'dogs' : 0}});
// update just the cats attribute
await petStore.record(key).update{'friends.cats' : 2};
// now I want to (if it is possible) remove the cats attribute without calling
await petStore.record(key).update({'name': 'fish', friends : {'dogs' : 0}})
最佳答案
is there a way to remove the key without updating the entire map?
是的,类似于Firestore,您可以使用哨兵值
FieldValue.delete
删除字段。使用点(
.
),您甚至可以引用嵌套字段。以下是在示例中删除cats
中的friends
键的示例:print(await petStore.record(key).get(db));
// prints {name: fish, friends: {cats: 0, dogs: 0}}
print(await petStore
.record(key)
.update(db, {'friends.cats': FieldValue.delete}));
// prints {name: fish, friends: {dogs: 0}}
有关更多信息,请查看有关updating fields的文档。
关于flutter - Sembast-删除 map 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63714306/