1

我有一个存储地图对象的 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}})
4

1 回答 1

1

有没有办法在不更新整个地图的情况下删除密钥?

是的,类似于 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}}

有关更多信息,请查看有关更新字段的文档

于 2020-09-02T22:52:11.473 回答