0

I migrated to null safety, environment: sdk: ">=2.12.0 <3.0.0"

But my ImageModel report error.

class ImageModel {
    ImageModel({
        this.total,
        this.totalHits,
    });

    int total;
    int totalHits;

    factory ImageModel.fromJson(Map<String, dynamic> json) => ImageModel(
        total: json["total"],
        totalHits: json["totalHits"],
    );

    Map<String, dynamic> toJson() => {
        "total": total,
        "totalHits": totalHits,
    };
}

Should I just add required to this.total and this.totalHits as the vscode suggest?

But this.total and this.totalHits are always return from server json.

Vincent
  • 3,124
  • 3
  • 21
  • 40
  • So how is the server json defined? *Can* it return `null` in those values? A mechanism like swagger should have that information. – nvoigt Apr 30 '21 at 05:46

1 Answers1

1

I would recommend you always to make all fields (except ids) nullable.

You never know what will happen in some months in the backend that one of those fields could start being nullable.

And some of ur users will never update the app.

Karim
  • 962
  • 7
  • 10