Flutter 如何在firebase和bloc中使用json和序列化?错误:将对象转换为可编码对象失败
这是我的云firestore,看起来像: 错误消息:未处理的异常:将对象转换为可编码的 对象失败:摄影 对我的数据库使用jsonSerializationFlutter 如何在firebase和bloc中使用json和序列化?错误:将对象转换为可编码对象失败,flutter,dart,google-cloud-firestore,bloc,Flutter,Dart,Google Cloud Firestore,Bloc,这是我的云firestore,看起来像: 错误消息:未处理的异常:将对象转换为可编码的 对象失败:摄影 对我的数据库使用jsonSerialization import 'package:json_annotation/json_annotation.dart'; part 'Model.g.dart'; @JsonSerializable() class Photography{ String couplePhoto; String female; String image_u
import 'package:json_annotation/json_annotation.dart';
part 'Model.g.dart';
@JsonSerializable()
class Photography{
String couplePhoto;
String female;
String image_url;
String info;
String male;
AllImages all_images;
Photography();
factory Photography.fromJson(Map<String, dynamic> json) => _$PhotographyFromJson(json);
Map<String,dynamic> toJson() => _$PhotographyToJson(this);
}
@JsonSerializable()
class AllImages {
List<String> imageUrl = List<String>();
AllImages();
factory AllImages.fromJson(Map<String, dynamic> json) => _$AllImagesFromJson(json);
Map<String,dynamic> toJson() => _$AllImagesToJson(this);
}
之后,我创建了DB类
如何使用模型类?
class DB {
final db = Firestore.instance;
// Stream<QuerySnapshot> initStream() {
// return db.collection('photography').snapshots();
// }
getPhotography() async {
return db.collection('photography')
.document("0yUc5QBGHNNq6WK9CyyF")
.setData(jsonDecode(jsonEncode(Photography)));
}
}
DB db = DB();
根据包裹来源:
/// Writes to the document referred to by this [DocumentReference].
///
/// If the document does not yet exist, it will be created.
///
/// If [merge] is true, the provided data will be merged into an
/// existing document instead of overwriting.
Future<void> setData(Map<String, dynamic> data, {bool merge = false}) {
return Firestore.channel.invokeMethod<void>(
'DocumentReference#setData',
<String, dynamic>{
'app': firestore.app.name,
'path': path,
'data': data,
'options': <String, bool>{'merge': merge},
},
);
}
根据包裹来源:
/// Writes to the document referred to by this [DocumentReference].
///
/// If the document does not yet exist, it will be created.
///
/// If [merge] is true, the provided data will be merged into an
/// existing document instead of overwriting.
Future<void> setData(Map<String, dynamic> data, {bool merge = false}) {
return Firestore.channel.invokeMethod<void>(
'DocumentReference#setData',
<String, dynamic>{
'app': firestore.app.name,
'path': path,
'data': data,
'options': <String, bool>{'merge': merge},
},
);
}
谢谢你回答我的问题。先生,不能使用Photography.toJson。photography.fromJson仅可供使用<代码>factory Photography.fromJson(Map json)=>;映射到JSON()=>u$PhotographyToJson(此)代码>我这样添加的,错误是未处理的异常:将对象转换为可编码对象失败:摄影
Map data=jsonDecode(jsonecode(摄影));Future postsMap=Firestore.instance.collection('photography').document(“0yUc5QBGHNNq6WK9CyyF”).setData(数据)代码>刚刚想到。。。方法是getPhotography
。。。此方法的目的是什么?抱歉,我无法更改方法名称..getPhotography set data的目的..它正在工作..但我不明白,如何从firestore检索数据?我在谷歌上搜索并找到这段代码,Photography.fromJson(jsonDecode(jsonEncode(snapshot.data))我不知道如何使用它。在哪里?什么是snapshot.data?ro如何在bloc上使用?谢谢你回答我的问题。先生,不能使用Photography.toJson。photography.fromJson仅可供使用<代码>factory Photography.fromJson(Map json)=>;映射到JSON()=>u$PhotographyToJson(此)代码>我这样添加的,错误是未处理的异常:将对象转换为可编码对象失败:摄影Map data=jsonDecode(jsonecode(摄影));Future postsMap=Firestore.instance.collection('photography').document(“0yUc5QBGHNNq6WK9CyyF”).setData(数据)代码>刚刚想到。。。方法是getPhotography
。。。此方法的目的是什么?抱歉,我无法更改方法名称..getPhotography set data的目的..它正在工作..但我不明白,如何从firestore检索数据?我在谷歌上搜索并找到这段代码,Photography.fromJson(jsonDecode(jsonEncode(snapshot.data))我不知道如何使用它。在哪里?什么是snapshot.data?ro在bloc上如何使用?@pskink先生,您有没有回答我的问题?@pskink先生,您有没有回答我的问题?
child: StreamBuilder<Photography>(
stream: bloc.outFirestore,
initialData: null,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: buildItem(snapshot.data, bloc));
// children: snapshot.data.documents
// .map<Widget>((doc) => buildItem(doc, bloc))
// .toList());
} else {
return SizedBox();
}
}),
buildItem(Photography doc, PhotographyBloc bloc) {
...
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: FadeInImage.assetNetwork(
placeholder: "assets/images/photography.jpg",
image: doc.couplePhoto,
// image: doc.data['couplePhoto'],
fit: BoxFit.fill,
),
),
/// Writes to the document referred to by this [DocumentReference].
///
/// If the document does not yet exist, it will be created.
///
/// If [merge] is true, the provided data will be merged into an
/// existing document instead of overwriting.
Future<void> setData(Map<String, dynamic> data, {bool merge = false}) {
return Firestore.channel.invokeMethod<void>(
'DocumentReference#setData',
<String, dynamic>{
'app': firestore.app.name,
'path': path,
'data': data,
'options': <String, bool>{'merge': merge},
},
);
}
getPhotography() async {
return db.collection('photography')
.document("0yUc5QBGHNNq6WK9CyyF")
.setData(photography.toJson());
}