Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null check error in Generated toJson methods in dart having nullable DateTime values #2590

Open
anugrahkora opened this issue May 10, 2024 · 0 comments
Labels
bug Dart Dart Output

Comments

@anugrahkora
Copy link

When model for a json having a nullable field is generated, the generated toJson Method doesn't recognise nullable DateTime value and returns an exception Null check operator used on a null value.

Null Safety

Output

Input Format: Json
Output Language: Dart

When we parse a large Json with multiple nullable date time object, the toJson Method causes the null check error and it is a lot of manual work to fix this issue

Use a null check before parsing the date on the toJson methods

Input Data

{
"debitNoteDate": "2023-01-01",
"dueDate": "2023-01-01"
}

Expected Behaviour / Output

Map<String, dynamic> toJson() => {

    "debitNoteDate": "${debitNoteDate!.year.toString().padLeft(4, '0')}-${debitNoteDate!.month.toString().padLeft(2, '0')}-${debitNoteDate!.day.toString().padLeft(2, '0')}",
   
    "dueDate": "${dueDate!.year.toString().padLeft(4, '0')}-${dueDate!.month.toString().padLeft(2, '0')}-${dueDate!.day.toString().padLeft(2, '0')}"
 };

Map<String, dynamic> toJson() => {

    "debitNoteDate": debitNoteDate==null? null:"${debitNoteDate!.year.toString().padLeft(4, '0')}-${debitNoteDate!.month.toString().padLeft(2, '0')}-${debitNoteDate!.day.toString().padLeft(2, '0')}",
   
    "dueDate":dueDate==null? null: "${dueDate!.year.toString().padLeft(4, '0')}-${dueDate!.month.toString().padLeft(2, '0')}-${dueDate!.day.toString().padLeft(2, '0')}"
 };

Current Behaviour / Output

Steps to Reproduce

Possible Solution

@inferrinizzard inferrinizzard added the Dart Dart Output label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Dart Dart Output
Projects
None yet
Development

No branches or pull requests

2 participants