我定义了一个带有一些字符串变量和另一个类B的对象列表的dart类A。在cloud_firestore模块中没有找到任何方法函数来将这个类A的自定义对象设置为集合的文档,即使自定义Firestore支持对象。我是否需要将A类的所有成员(字符串,列表等)转换为JSON格式或任何其他可用的解决方案?
如果创建相关的自定义类的另一个集合很不方便,唯一的选择是自己构建相关的类实例。
import Player from // player module
class GameState {
constructor(data) {
this.players = data.players.map(p => new Player(p))
// ...
}
// flatten GameState into a generic JS map
// build one of these on Player also
asFBData() {
const playerFBData = this.players.map(p => p.asFBData())
return { playerFBData, ...other_game_state_here }
}
}
const gameStateConverter = {
toFirestore: gameState => gameState.asFBData(),
fromFirestore: (snapshot, options) => new GameState(snapshot.data(options))
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句