9

The following is the code:

    static TodoState fromJson(json) {
          JsonCodec codec = new JsonCodec();
            List<Todo> data = codec.decode(json["todos"]);
            VisibilityFilter filter = codec.decode(json['visibilityFilter']);

     return new TodoState(todos: data,
                          visibilityFilter: filter);
  }

Error produced by Android Studio:

[VERBOSE-2:dart_error.cc(16)] Unhandled exception: FormatException: Unexpected character (at character 3)

Any idea how to make it work?

enter image description here

This is the output of the Json as produced by Redux.

13
  • What does your json look like? Apr 4, 2018 at 11:45
  • it is auto made by redux; with redux persist, it is basically an array with objects inside, the objects have an id(int), text(string), a completed(bool) Apr 4, 2018 at 11:47
  • codec.decode(json["todos"].replace("\ufeff", "")); if it were a UTF-8 BOM.
    – Joop Eggen
    Apr 4, 2018 at 11:50
  • i dont need to replace it, just pass it to a list to reused, and save . data locally. Apr 4, 2018 at 11:52
  • Please post the output of the JSON you get from Redux Apr 4, 2018 at 11:59

4 Answers 4

15

There's a problem with your code as well as the string you're trying to parse. I'd try to figure out where that string is being generated, or if you're doing it yourself post that code as well.

Valid Json uses "" around names, and "" around strings. Your string uses nothing around names and '' around strings.

If you paste this into DartPad, the first will error out while the second will succeed:

import 'dart:convert';

void main() {
  JsonCodec codec = new JsonCodec();
  try{
    var decoded = codec.decode("[{id:1, text:'fdsf', completed: false},{id:2, text:'qwer', completed: true}]");
    print("Decoded 1: $decoded");
  } catch(e) {
    print("Error: $e");
  }

  try{
    var decoded = codec.decode("""[{"id":1, "text":"fdsf", "completed": false},{"id":2, "text":"qwer", "completed": true}]""");
    print("Decoded 2: $decoded");
  } catch(e) {
    print("Error: $e");
  }
}

The issue with your code is that you expect the decoder to decode directly to a List. It will not do this; it will decode to a dynamic which happens to be a List<dynamic> whose items happen to be Map<String, dynamic>.

See flutter's Json documentation for information on how to handle json in Dart.

3
13

If Anyone came here and your are using dio package to call http request you need to set responseType to plain

 BaseOptions options = new BaseOptions(
    baseUrl: "<URL>",
    responseType: ResponseType.plain
  );
1
  • thanks a lot @Bawantha... u saved my time. Sep 23, 2021 at 16:58
11

I don't know if that's the case, but I got a similar error when me JSON looks like this

[ { ... }, ]

and not like this

[ { ... } ]

The comma was causing the issue.

0
1

I also have similar type of error, Be make sure that the argument of .decode method shouldn't be empty object.

try {
if(json["todos"].isNotEmpty) {
List<Todo> data = codec.decode(json["todos"]);
}
if(json["todos"].isNotEmpty) {
VisibilityFilter filter = codec.decode(json['visibilityFilter']);
}
}
catch(e) {
print(e);
}

Do try this, hope it will work for you.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.