161

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?

6
  • 16
    Why not simply using breakpoints instead ? Commented Mar 6, 2018 at 20:04
  • 2
    Have you tried if flutter logs or adb logcat provides full output? Commented Mar 6, 2018 at 20:20
  • 5
    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... Commented Mar 7, 2018 at 20:44
  • 1
    flutter logs displays the same, that's actually where the screenshot comes from. Commented Mar 7, 2018 at 20:45
  • Confirmed working by using debugPrint(someSuperLongString, wrapWidth: 1024); Commented May 25, 2023 at 23:56

13 Answers 13

243

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:

import 'dart:developer';


log(reallyReallyLongText);

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

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

9 Comments

what am I doing wrong ? var data = { 'recordType':recordTypeText, 'horseId': horseId, 'dateOfCheck': dateOfCheck, 'title': title, 'details': details, 'images':images }; log(data.toString()); and no output
doesn't work for me still getting trucated
@user1634451 its a different level of output, you might not see it on the terminal but it works on vscode
it works!!!.. any probs importing the developer import ?
I am still seeing no output in terminal in my VSCODE, I don't understand why there are so many upvote , or in latest version this log is not working? or anyone please state more specific steps on how to check the printed value? There are tabs like "Terminal" , "OUTPUT" , "DEBUG CONSOLE" , "PROBLEMS" , should I find it in OUTPUT or how?
|
82

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

Comments

27

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

debugPrint(someSuperLongString, wrapWidth: 1024);

3 Comments

it prints everything but again result is being truncated...
What do you mean, Jan? :-? If it still truncates, how does it "print everything"?
It splits the log on console, but still doesn't print the large log completely!
18

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));
       }
    }
}

1 Comment

1023 actually. That's 1 KB - 1 character. :)
8

I have the perfect solution,It can be parsed no matter how long it is, and it is still in json format, Can be easily parsed or copied for use

import 'dart:developer';
import 'dart:convert';
...
...
log('${jsonEncode(responseData)}');

1 Comment

log function doesn't work when you use the flutter run command.
7

Here is a one-liner based on @CopsOnRoad's answer that you can quickly copy and paste (such as: when you want to slightly modify your code and log some data and see temporarily):

void printWrapped(String text) => RegExp('.{1,800}').allMatches(text).map((m) => m.group(0)).forEach(print);

Comments

5

You can use Flutter DevTools. It has a Logging View section with logs containing full string.

https://docs.flutter.dev/tools/devtools/logging

Comments

3

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.

5 Comments

I also read that and I did try debugPrint to no avail. I'm also running my app on the iPhone simulator right now, and there's nothing about logs being cut off when developing for iOS.
hmm, thanks for letting me know about this. I will try and check once again and run it on iosSimulator.
debugPrint() also truncates long strings for me. Limited seems to be 700 characters.
Yes me also still got truncated using debugprint
just need to add wrapWidth: <SomeBigInt> calling debugPrint and it will not truncate
3

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

debugPrint and print are actually truncating the output.

Comments

2

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.

3 Comments

It's still not working for me :/
Didn't Work for me
still get truncate. Didn't work
1

Method 1

   void prints(var s1) {
        String s = s1.toString();
        debugPrint(" =======> " + s, wrapWidth: 1024);
      }

Method 2

void prints(var s1) {
  String s = s1.toString();
  final pattern = RegExp('.{1,800}');
  pattern.allMatches(s).forEach((match) => print(match.group(0)));
}

Just call this method to print your longggg string

Comments

-1

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.

Comments

-1

Same issue caused lot of frustration when I have to test base64 of images. I was using iTerm2 editor, so the answer is specific to the iTerm2

 1. Navigate to Preferences -> Profiles
 2. Select your Profile (in my case was **Default** only)
 3. Select **Terminal** in the header of right pan
 4. Check Unlimited scrollback

Now you can have copy the large strings from the terminal.

5 Comments

where is this option in android studio ?
The above answer is specific to iTerm2. For Android studio, you can do so from: Settings -> Tools -> Logcat -> Logcat cycle buffer size
there is no logcat inside tools
@VipinVerma, double-check if you are looking for Tools inside Settings of the Android Studio, it shouldn't be the one in the toolbar.

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.