62
votes

I'm trying to build a Flutter App and learning Dart in the process, but I'm getting kind of frustrated when debugging. I have fetched a resource from an API and now I want to print the JSON string to the console, but it keeps cutting off the string.

Screenshot of the cut off string in the console

So I actually have two questions: is the terminal console really the only way to print debug messages and how can I print large strings to the console without them automatically getting cut off?

9
Why not simply using breakpoints instead ?Rémi Rousselet
Have you tried if flutter logs or adb logcat provides full output?Günter Zöchbauer
Breakpoints are a good suggestion, but still, it shouldn't be impossible to just print the whole response JSON string to the console without pausing my app? Breakpoints are great, but not always what you want...Terrabythia
flutter logs displays the same, that's actually where the screenshot comes from.Terrabythia

9 Answers

82
votes

How about using the Flutter log from the dart: developer library. This does not seem to have the maximum length limit like print() or debugPrint(). This is the only solution that seems to work fine. Try it as below:

log(reallyReallyLongText)

The output will be the entire long string without breaks and prefixed with [log]

36
votes

You can make your own print. Define this method

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 ...");

Credit

15
votes

Currently dart doesn't support printing logs more than 1020 characters (found that out by trying).

So, I came up with this method to print long logs:

static void LogPrint(Object object) async {
    int defaultPrintLength = 1020;
    if (object == null || object.toString().length <= defaultPrintLength) {
       print(object);
    } else {
       String log = object.toString();
       int start = 0;
       int endIndex = defaultPrintLength;
       int logLength = log.length;
       int tmpLogLength = log.length;
       while (endIndex < logLength) {
          print(log.substring(start, endIndex));
          endIndex += defaultPrintLength;
          start += defaultPrintLength;
          tmpLogLength -= defaultPrintLength;
       }
       if (tmpLogLength > 0) {
          print(log.substring(start, logLength));
       }
    }
}
12
votes

Use debugPrint with the optional parameter to wrap according to the platform's output limit.

debugPrint(someSuperLongString, wrapWidth: 1024);
3
votes

There is an open issue for that: https://github.com/flutter/flutter/issues/22665

debugPrint and print are actually truncating the output.

2
votes

Please try debugPrint('your output'); instead of print('your output'); the documentation is here if you would like to read. debugPrint throttles the output to a level to avoid being dropped by android's kernel as per the documentation.

2
votes

You can achieve this using the Logger Plugin: https://pub.dev/packages/logger

To print any type of log Just do the do the following.

  var logger = Logger();

  logger.d("Logger is working!");// It also accept json objects

In fact, it will even format the output for you.

1
votes

Use this method

JsonEncoder encoder = new JsonEncoder.withIndent('  ');
String prettyprint = encoder.convert(yourJsonString);
debugPrint(prettyprint);

But it works only with JSON strings, I have not idea about how to do with "normal" strings. The first thing I think is to add manually a \n after every n characters, for now, while waiting for a stable fix!

0
votes

If you run the application in android studio it will truncate long string.

In xcode 10.2 which i am using long string is not truncating.

My suggestion is write print statement logs and run the application in Xcode instead of android studio.