0

According to my previous question I'm already solve the null safety problem, but here i got the new problem about initializer on my model, enter image description herehere's the short code of my model looks like and I got the error from here:

 LeaveListResponse({
    this.total,
    this.result,
  });

  LeaveListResponse.fromJson(Map<String, dynamic> json) {
    total = json['total'] as int?;
    result = (json['result'] as List?)
        ?.map((dynamic e) =>
            LeaveListResponse.fromJson(e as Map<String, dynamic>))
        .toList();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> json = <String, dynamic>{};
    json['total'] = total;
    json['result'] = result?.map((e) => e.toJson()).toList();
    return json;
  }
}

class LeaveListResult {
  int? paidLeaveId;
  String? paidLeaveUuid;
  String? paidLeaveEmployeeUuid;
  String? paidLeaveEmployeeNip;
  String? paidLeaveEmployeeFullName;
  String? paidLeaveTypeUuid;
  String? paidLeaveTypeName;
  String? paidLeaveTypeDetailUuid;
  String? hirarki;
  int? levels;
  int? jumlahHari;

I'm calling the model inside:

LeaveListModel responses =
    snapshot.data!.data as LeaveListModel;
    return Text(responses.response?.paidLeaveTypeName?? "");

so after I put the null safety inside the Text() it start to showing the error on my model, how to initialize paidLeaveTypeName?

1988
  • 309
  • 2
  • 15
  • Your error does not match your code. – jamesdlin Nov 18 '22 at 04:41
  • I'm edit the answer, you can see the picture of the error – 1988 Nov 18 '22 at 04:48
  • 1
    The error says you have a non-nullable field named `paidLeaveTypeName`. The code you've shown does not show any such field (but it does show a *nullable* field named `paidLeaveTypeName`, although based on your screenshot, that's in a different class). The error message also tells you what to do: either initialize `LeaveListResponse`'s `paidLeaveTypeName` member or make it nullable. – jamesdlin Nov 18 '22 at 04:54
  • if you want to see the full code of the widget, you can see it on here https://pastecode.io/s/ziweo7g3, but if you don't mind can you show me how to initialize it? because I keep getting an error when I initialized the paidLeaveTypeName. – 1988 Nov 18 '22 at 05:12
  • 2
    See [How do I initialize non-nullable members in a constructor body?](https://stackoverflow.com/q/66725613/). If you still need help, ask a specific question with specific details about why none of those approaches work for you. – jamesdlin Nov 18 '22 at 05:45
  • I still got an error after do all of the step from the answer that you have give to me, I'm already try with adding late constructor, initializer list or adding nullable constructor to my model class just like this https://pastecode.io/s/gud6vgvu – 1988 Nov 18 '22 at 06:24
  • You haven't shown exactly what you tried and why it doesn't work. – jamesdlin Nov 18 '22 at 06:34
  • I've added initializer list into my model class just like in the bottom side of this code pastecode.io/s/gud6vgvu and I've try to delete the nullable constructor then write the initializer just like String paidLeaveTypeName = ''; and the error is still the same "Non-nullable instance field 'paidLeaveTypeName' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.", but thank you for showing me how to initialize it anyway – 1988 Nov 18 '22 at 06:41
  • You have your classes mixed up. As I said earlier: your problem is with the `LeaveListResponse` class, not with `LeaveListResult`. – jamesdlin Nov 18 '22 at 06:57

0 Answers0