Unhandled Exception: type 'Future' i

2019-08-23 06:38发布

问题:

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;
}

The error message is:

Unhandled Exception: type 'Future< void >' is not a subtype of type 'Future< Photography> '

Photography model class

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);
}

And next problem is, how to add to these data on bloc sink?

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

something like this? but error is, The argument type 'Future' can't be assigned to the parameter type 'Photography'

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

回答1:

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.)