0

Getting errors after trying to convert nested JSON data using dart's factory concept. Here I have two classes to handle the json data, but still getting this error:

Exception has occurred. _TypeError (type 'FormatException' is not a subtype of type 'Map')

Here is the code:


    class BodyResponse {
      final Map<String, dynamic> message;
      BodyResponse({
        this.message
      });
      factory BodyResponse.fromJson(Map<String, dynamic> json) {
        return BodyResponse(message: json['message']);
      }
    }

    class ErrorResponse {
      final BodyResponse body;
      final int status;
      final String contentType;
      final bool success;

      ErrorResponse({
        this.body, this.status, this.contentType, this.success
      });

      factory ErrorResponse.fromJson(Map<String, dynamic> json) {
        return ErrorResponse(
          body: BodyResponse.fromJson(json['body']),
          status: json['status'],
          contentType: json['content_type'],
          success: json['success']
        );
      }
    }

    ErrorResponse errors = ErrorResponse.fromJson("""
      {
        "body": {
          "message": "Some one has already taken this username(fooBar), please try again with a new username."
        },
        "status": 500,
        "content_type": "application\/json",
        "success": false
      }
    """);

    print(errors);

What could go wrong here?

2
  • The value you pass as an argument at ErrorResponse errors = ErrorResponse.fromJson(...) seems to be a string, while the function expects it to be a map (Map<String, dynamic> json). Commented Jun 8, 2019 at 12:22
  • @George but still there is no way to pass the JSON then! Commented Jun 8, 2019 at 12:37

1 Answer 1

1

Modified most of your code here. Hope that this is what you tried to achieve.

import 'dart:convert';

class BodyResponse {
  final String message;
  BodyResponse({this.message});

  BodyResponse.fromJson(Map<String, dynamic> json):
    message = json['message'];

  factory BodyResponse.fromString(String encodedJson) {
    return BodyResponse.fromJson(json.decode(encodedJson));
  }

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

  String toString() => json.encode(this.toJson());

}

class ErrorResponse {
  final BodyResponse body;
  final int status;
  final String contentType;
  final bool success;

  ErrorResponse({this.body, this.status, this.contentType, this.success});

  ErrorResponse.fromJson(Map<String, dynamic> json):
    body = BodyResponse.fromJson(json['body']),
    status = json['status'],
    contentType = json['content_type'],
    success = json['success'];

  factory ErrorResponse.fromString(String encodedJson) {
    return ErrorResponse.fromJson(json.decode(encodedJson));
  }

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

  String toString() => json.encode(this.toJson());

}

void main() {
  ErrorResponse errors = ErrorResponse.fromString("""
      {
        "body": {
          "message": "Some one has already taken this username(fooBar), please try again with a new username."
        },
        "status": 500,
        "content_type": "application\/json",
        "success": false
      }
    """);
  print(errors);
}

Let me know if this helped.

Sign up to request clarification or add additional context in comments.

6 Comments

You solution works just fine when i run it in a dart environment. But when i am trying to use this inside a catch block again it seems to break :( Here is a screenshot: i.sstatic.net/WnOUe.png
I've tried with both ErrorResponse.fromJson and ErrorResponse.fromString method
Also, the JSON somehow getting an extra { at the end of the string! i.sstatic.net/rcp9G.png
@rakibtg you are trying to get the server error response from local Dart exception (e). I believe you should use the same response.body with fromString method instead.
Thanks George for your time. From my JS knowledge i was trying with this try...catch block: i.imgur.com/07BUPdg.png Do you think according to dart it is possible? :)
|

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.