5

I have this Firebase Document that I want to convert to a JSON and add Id to it when using it within the app.

factory Recipe.fromDocument(DocumentSnapshot doc) {
    final data = doc.data()!;
    return Recipe.fromJson(data).copyWith(id: doc.id);
  }

I get the following error enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Millenial2020
  • 2,465
  • 9
  • 38
  • 83

1 Answers1

3

Try this

factory Recipe.fromDocument(DocumentSnapshot doc) {
    final data = doc.data()! as Map<String, dynamic>;
    return Recipe.fromJson(data).copyWith(id: doc.id);
  }

According to the FlutterFire usage documentation

DocumentSnapshot doc;
doc.data() is of type Map<String, dynamic>;
Sahil Hariyani
  • 1,078
  • 3
  • 11