Flutter CloudFirestore添加复杂类型会引发无效参数:“团队”实例 [英] Flutter CloudFirestore adding complex types throws Invalid argument: Instance of 'Team'

查看:106
本文介绍了Flutter CloudFirestore添加复杂类型会引发无效参数:“团队”实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Match类,其中有Team类型的其他类的列表。仅当列表为空时,才可以将新文档(匹配)上载到Firestore。列表中有团队项目后,尝试将其添加到Firestore中,并抛出 无效参数:团队的实例。团队类仅包含简单的字段,而没有其他类。

I have class Match that has a list of other classes of type Team. Upload of new document (Match) to Firestore is only possible if the list is empty. Once there is Team item in the list, attempt to add it to Firestore throws 'Invalid argument: Instance of 'Team''. Team class only contains simple fields, no other classes.

从StandardMessageCodec.writeValue

Exception is thrown from StandardMessageCodec.writeValue

更新:
我正在使用cloud_firestore:^ 0.8.2,并调用

Update: I am using cloud_firestore: ^0.8.2, and calling

await firestore.collection('matches').document(match.id).setData(match);

因为除了setData(接受Map)之外没有其他方法可以上传,所以我有尝试过match.teams(列表):

since there is no other method that would allow for upload other than setData (accepting Map), I have tried for match.teams (List):


  1. 'teams':团队(列表)

  2. 'teams':完全编码为json(字符串)

  3. 'teams':编码各个Team项目并添加到集合中(列表)

第一个选项将失败,第二个将创建单个字符串文档成员,第三个将在成员内部创建字符串数组,这些都不是我想要的-在文档内部创建团队对象数组

1st option will fail, second will create a single string document member and third will create an array of strings inside member, none of which is what I want - create an array of team objects inside document

推荐答案

要完全解决上述问题,请执行以下步骤:

To Completely solve the above problem, follow the following steps:

步骤:


  1. 使用json_serializable插件(链接: https://pub.dev/packages/json_serializable

在以下类级别json_serializable和Match类的注释(请注意选项)

At the following class level json_serializable annotation to your Match class (note the options)

部分 match.g.dart; //会自动生成实际文件

part 'match.g.dart'; //actual file will be automatically generated

@JsonSerializable(anyMap:true,explicitToJson:true)

@JsonSerializable(anyMap: true, explicitToJson: true)

对您的Team类执行相同的操作(在上面的2中)(您可以忽略以下选项:explicitToJson):

Do the same(in 2 above) for your Team class(you can ignore the option: explicitToJson):

part'team.g.dart'; //将自动生成

part 'team.g.dart'; //will be automatically generated

@JsonSerializable(anyMap:true)

@JsonSerializable(anyMap: true)

或控制台(在项目的根目录),运行: flutter pub run build_runner build 以生成所有必需的* .g.dart文件(例如match.g.dart,team.g.dart)为您的序列化类文件

in your terminal or console (at the root of your project), run: flutter pub run build_runner build to generate all the necessary: *.g.dart files (i.e. match.g.dart, team.g.dart) for your serialized class files

[pc-name]:[project_root_dir] [username} $ flutter pub run build_runner build

[pc-name]:[project_root_dir] [username}$ flutter pub run build_runner build

我真的希望这能像您的发现一样完全解决您的问题。

I really hope this helps solve your problem completely as it did mine.

参考: https://flutter.dev/docs/development/data-and-backend/json

这篇关于Flutter CloudFirestore添加复杂类型会引发无效参数:“团队”实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆