未处理的异常:类型为'Future< void>'不是“ Future< Photography>”类型的子类型在Firestore上 [英] Unhandled Exception: type 'Future<void>' is not a subtype of type 'Future<Photography>' on Firestore
问题描述
我需要从Firestore数据库中获取所有数据。
我遵循这种方式, https://stackoverflow.com/a/55865045/9139407
I need to get all the data from the firestore database. I follow this way, https://stackoverflow.com/a/55865045/9139407
Future<Photography> getPhotography() {
Future<Photography> data = await db
.collection('photography')
.document('0yUc5QBGHNNq6WK9CyyF')
.setData(jsonDecode(jsonEncode(Photography(
[AllImages(["list"])],
"image_url",
"Shanika",
"image_url",
"lovely couple",
"Bhanuka"
))));
return data;
}
错误消息是:
未处理的异常:键入'Future< void>不是
Future<类型的子类型摄影>'
Unhandled Exception: type 'Future< void >' is not a subtype of type 'Future< Photography> '
摄影模型类
import 'package:json_annotation/json_annotation.dart';
part 'Model.g.dart';
@JsonSerializable()
class Photography{
List<AllImages> all_images;
String couplePhoto;
String female;
String image_url;
String info;
String male;
Photography();
factory Photography.fromJson(Map<String, dynamic> json) => _$PhotographyFromJson(json);
Map<String,dynamic> toJson() => _$PhotographyToJson(this);
}
@JsonSerializable()
class AllImages {
final List<String> imageUrl;
AllImages(this.imageUrl);
factory AllImages.fromJson(Map<String, dynamic> json) => _$AllImagesFromJson(json);
Map<String,dynamic> toJson() => _$AllImagesToJson(this);
}
下一个问题是,如何在bloc上添加这些数据
Sink<Photography> get inFirestore => _firestoreController.sink;
像这样吗?但错误是,不能将参数类型'Future'分配给参数类型'摄影'
final result = db.getPhotography();
inFirestore.add(result);
推荐答案
Document.setData()
返回 Future< void>
-表示将来完成时不会返回任何数据。这当然对二传手来说是合理的。这就是为什么您无法将其变成任何东西的原因。目前尚不清楚您打算从何处获取数据
。您从一个空的(?)摄影
开始,对其进行编码,解码并存储,然后期望返回某些内容。什么?原来的空物体? (您可以退还-但我认为那不是您想要的。)
Document.setData()
returns a Future<void>
- meaning that when it completes in the future it won't return any data. That's reasonable for a setter of course. That's why you can't turn it into anything. It's not clear where you intend to get data
from. You start with an empty(?) Photography
, encode it, decode it, and store it then expect to return something. What? The original empty object? (You could just return that - but I don't think that's what you want.)
这篇关于未处理的异常:类型为'Future< void>'不是“ Future< Photography>”类型的子类型在Firestore上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!