Sembast - 删除地图中的值

Sembast - remove a value in a map

我有一个存储地图对象的 sembast 商店。如果我想删除地图中的键,我是否必须更新整个地图对象,或者有没有办法在不更新整个地图的情况下删除键?文档只展示了如何删除记录,而不是如何删除字段。既然有更新单个字段的方法,我觉得把这个特性用于其他操作是有意义的。

示例:

// 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

使用点 (.),您甚至可以引用嵌套字段。以下是在您的示例中删除 friends 中的 cats 键的示例:

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

的文档