【问题标题】:Unhandled Exception: type 'Future<void>' is not a subtype of type 'Future<Photography>' on Firestore未处理的异常:类型“Future<void>”不是 Firestore 上“Future<Photography>”类型的子类型
【发布时间】:2019-11-10 21:25:37
【问题描述】:

我需要从 firestore 数据库中获取所有数据。 我按照这个方式,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' 不是类型的子类型 '未来'

摄影模型类

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 上添加这些数据?

  Sink<Photography> get inFirestore => _firestoreController.sink;

像这样?但是错误是,参数类型“Future”不能分配给参数类型“Photography”

final result = db.getPhotography();
inFirestore.add(result);

【问题讨论】:

    标签: flutter dart google-cloud-firestore bloc


    【解决方案1】:

    Document.setData() 返回一个Future&lt;void&gt; - 这意味着当它在未来完成时它不会返回任何数据。这对于二传手当然是合理的。这就是为什么你不能把它变成任何东西。目前尚不清楚您打算从哪里获得data。你从一个空的(?)Photography 开始,对其进行编码、解码并存储它,然后期望返回一些东西。什么?原来的空对象? (你可以直接返回 - 但我认为这不是你想要的。)

    【讨论】:

    • 添加了我的模型类。能告诉我如何将 Firestore 数据设置到我的模型类吗?
    • 如何获取数据?
    • 链接问题中描述的方法似乎有点矫枉过正。将 Firestore 快照与 JSON 转换是相当奇怪的。我宁愿让你的班级知道如何从快照中读取自己。它主要是字符串,很容易。图像列表可能被建模为 firestore 中的数组,但您没有显示 AllImages 类中的内容。 (如果确实存在例如 URL,则名称选择不当。)
    猜你喜欢
    • 2020-06-14
    • 2020-07-08
    • 2021-01-12
    • 2020-11-19
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多