0

Flutter is giving me this error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected end of input (at character 88586)
[        ] E/flutter ( 5286): ...820457458, -0.09168463200330734, 0.9341723918914795, 0.9910492897033691]}",

This is how I am inflating the list (that is later written to a JSON file):

for(var i = 0 ; i <=32; ++i) {
            String entry = '{contents: ['
                '${contentList.content[i].x.toString()}, '
                '${contentList.content[i].y.toString()}, '
                '${contentList.content[i].z.toString()}, '
                '${contentList.content[i].visibility.toString()}, '
                '${contentList.content[i].presence.toString()}]}';
            content_collector.add(entry);
          }

content_collector is then passed to this function to write to a JSON file:

  Future<File> saveToJSONFile(List<String> contents_list, String filename) async {
    String encodedContents = jsonEncode(contents_list);
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path + '/' + filename;

    return await File(appDocPath).writeAsString(encodedContents);
  }

and I have this function to read the generated JSON file:

void readJSON(String path) async {

    Directory appDocDir = await getApplicationDocumentsDirectory();
    String appDocPath = appDocDir.path + '/' + path;

    String response = await File(appDocPath).readAsString();
    final data = await json.decode(response.toString());

  }

So really, it is the readJSON function that throws this error.

6
  • You should in general not create JSON manually but instead use jsonEncode. But without the JSON you are trying to parse, it is hard to know fore sure what problem you have. One guess would be you have forgot to put " around contents. Commented Jun 14, 2022 at 7:40
  • @julemand101 I am using jsonEncode. This entry is sent to a function that invokes jsonEncode to turn it into JSON. The error says that there is soemthing wrong with my formatting. Commented Jun 14, 2022 at 7:43
  • Again. You should not create JSON manually which is clearly what you are doing here. Your method should just return a list of objects which each implement a toJson() method that tells how it should be encoded. But if you want to create JSON manually, you should ensure that contents is "contents". Commented Jun 14, 2022 at 7:45
  • @julemand101 Can you show me an example? Commented Jun 14, 2022 at 7:46
  • Can you at least show an example of how you want your JSON to look like so I have something to base my example on? Commented Jun 14, 2022 at 7:48

2 Answers 2

3

This part is not valid JSON

{contents: [

it should be

{"contents": [

Most likely the other parts needs to be surrounded with " as well

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

Comments

0

As @julemand101 mentioned, this is not a proper approach, but also looking at the code, the string you are crating, is not being concatenated either. Try this:

 String entry = "{'contents': [
                '${contentList.content[i].x.toString()}', 
                '${contentList.content[i].y.toString()}',
                '${contentList.content[i].z.toString()}',
                '${contentList.content[i].visibility.toString()}',
                '${contentList.content[i].presence.toString()}'
               ]}";

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.