vonAlenberg
vonAlenberg

Reputation: 602

Flutter reads JSON just partially

I'm using Flutter / Dart to create a little app, and it works quite fine. My assets are correctly defined in pubspec.yaml.

However, when I try to load data from assets (JSON) it only loads half of the data.

appData.json

{
"plan": [
    {
        "name": "plan_demo",
        "author": "A Name",
        "version": 1,
        "times_completed": 0,
        "percent_done": 0,
        "last_date_read": "1970-01-09",
        "missedDate": [
            {
                "date": "1970-01-01"
            },
            {
                "date": "1970-01-05"
            }
        ],
        "part": [
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 1,
                "endVerse": 5
            },
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 6,
                "endVerse": 10
            }
        ]
    },
    {
        "name": "another plan",
        "author": "Another Name",
        "version": 1,
        "times_completed": 0,
        "percent_done": 0,
        "last_date_read": "2020-12-28",
        "missedDate": [
            {
                "date": "2020-11-01"
            }
        ],
        "part": [
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 1,
                "endVerse": 5
            },
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 6,
                "endVerse": 10
            }
        ]
    }
  ]
}

The calling method looks like this:

@override
void initState() {
  super.initState();

  rootBundle.loadString("assets/appData.json").then((value) {
    print('rootBundle : ' + value);
  });
}

The output now is:

I/flutter (20595): rootBundle : {
I/flutter (20595):     "plan": [
I/flutter (20595):         {
I/flutter (20595):             "name": "plan_demo",
I/flutter (20595):             "author": "A Name",
I/flutter (20595):             "version": 1,
I/flutter (20595):             "times_completed": 0,
I/flutter (20595):             "percent_done": 0,
I/flutter (20595):             "last_date_read": "1970-01-09",
I/flutter (20595):             "missedDate": [
I/flutter (20595):                 {
I/flutter (20595):                     "date": "1970-01-01"
I/flutter (20595):                 },
I/flutter (20595):                 {
I/flutter (20595):                     "date": "1970-01-05"
I/flutter (20595):                 }
I/flutter (20595):             ],
I/flutter (20595):             "part": [
I/flutter (20595):                 {
I/flutter (20595):                     "isDone": false,
I/flutter (20595):                     "startBook": 1,
I/flutter (20595):                     "endBook": 1,
I/flutter (20595):                     "startChapter": 1,
I/flutter (20595):                     "endChapter": 1,
I/flutter (20595):                     "startVerse": 1,
I/flutter (20595):                     "endVerse": 5
I/flutter (20595):                 },
I/flutter (20595):                 {
I/flutter (20595):                     "isDone": false,
I/flutter (20595):                     "startBook": 1,
I/flutter (20595):                     "endBook": 1,
I/flutter (20595):                     "startChapter": 1,
I/flutter (20595):                     "endChapter": 1,
I/flutter (20595):                     "startVerse":

Why is my data cut off after some point in the file?

Upvotes: 0

Views: 604

Answers (3)

Mohammad Kurjieh
Mohammad Kurjieh

Reputation: 1153

As mentioned by the other answers, the problem is the truncation in the print statement. A lot of methods where discussed to overcome this issue here and here. But I would like to propose a different approach for your case, we can use inspect() by importing it from dart:developer. This adds folding and works similar to printing an object in the console of the browser. Link

Example

rootBundle.loadString("assets/appData.json").then((value) {
  inspect(jsonDecode(value));
});

This works well in this case as it is more practical to use folding over printing the entire JSON as a single string.

Upvotes: 1

Mukul
Mukul

Reputation: 1155

A slightly nicer workaround might be to create your own print that splits into smaller chunks, then you don't need to modify the code for each long string. Eg.:

void printWrapped(String text) {
  final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
  pattern.allMatches(text).forEach((match) => print(match.group(0)));
}

Use it like

printWrapped("Your very long string ...");

Credits and For More Solution to print a Long String in flutter See This Thread

Upvotes: 0

vonAlenberg
vonAlenberg

Reputation: 602

Following the link(s) from @GrahamD a possible solution is using

debugPrint(someSuperLongString, wrapWidth: 1024);

With the optional argument the output is not truncated. With the normal print function truncation happens, even though editors don't show that like in my case.

Upvotes: 1

Related Questions