1

I have a signup form and the backend is PHP. PHP returning success, error data as JSON.

Below is my JSONParse file.

import 'dart:convert';

Signup signupFromJson(String str) => Signup.fromJson(json.decode(str));

String signupToJson(Signup data) => json.encode(data.toJson());

class Signup {
    Errors errors;
    bool success;

    Signup({
        this.errors,
        this.success,
    });

    factory Signup.fromJson(Map<String, dynamic> json) => Signup(

        errors: Errors.fromJson(json["errors"]),
        success: json["success"],
    );

    Map<String, dynamic> toJson() => {
        "errors": errors.toJson(),  
        "success": success,
    };
}

class Errors {
    String password1;
    String email1;
    String name;

    Errors({
        this.password1,
        this.email1,
        this.name,
    });

    factory Errors.fromJson(Map<String, dynamic> json) => Errors(
        password1: json["Password1"],
        email1: json["Email1"],
        name: json["Name"],
    );

    Map<String, dynamic> toJson() => {
        "Password1": password1,
        "Email1": email1,
        "Name": name,
    };
}

Here is sample JSON Data.

If login is failed due to some condition not met.

{"errors":{"Email1":"Email could not be empty",
"Password1":"Password could not be empty",
"Name":"Your name must be between 3 to 30 characters!"},
"success":false}

If login successful

{"success":false}

My JSONParse is working properly if condition not met. I mean it retuning all the errors and Success false.

But if login is successful then it is not working, by checking more I found data is coming fine from PHP.

Signup signupFromJson(String str) => Signup.fromJson(json.decode(str)); - Data is coming in str variable.

factory Signup.fromJson(Map<String, dynamic> json) => Signup(

            errors: Errors.fromJson(json["errors"]), -- If error is nulll then it is not checking the success.
            success: json["success"],
        );

The issue coming here when errors are null then it moved out didn't goto the success section.

What am I doing wrong here?

6
  • why don't you just add a condition like json["errors"] ?? "" this will check the value and return "" if the value is null Commented May 3, 2020 at 4:41
  • @VickySalunkhe thanks for the comment where exactly you want me to add it. Commented May 3, 2020 at 4:44
  • the line where you added a comment If error is nulll then it is not checking the success. Commented May 3, 2020 at 4:45
  • I tried this errors: Errors.fromJson(json["errors"] ?? null), but it is still not working. It is still skipping success. Commented May 3, 2020 at 5:00
  • 1
    also have a look at this, I have already posted an answer which kind of fits here (if you looking forward for how to deal with json) stackoverflow.com/a/58708634/9236994 Commented May 3, 2020 at 5:11

1 Answer 1

2

If you get success then you will not get errors, so you have to check null aware where you are passing to error class too.

In Signup.fromJson :

 errors: Errors.fromJson(json["errors"] ?? {}),

In Error Class

 factory Errors.fromJson(Map<String, dynamic> json) => Errors(
        password1: json["Password1"] ?? '',
        email1: json["Email1"] ?? '',
        name: json["Name"] ?? '',
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.