125

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?

Terrabythia
  • 2,031
  • 3
  • 19
  • 29

13 Answers13

179

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]

Sisir
  • 4,584
  • 4
  • 26
  • 37
  • 9
    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 – marhyno May 05 '20 at 15:15
  • 5
    doesn't work for me still getting trucated – user1634451 Apr 01 '21 at 05:15
  • 3
    @user1634451 its a different level of output, you might not see it on the terminal but it works on vscode – Edoardo Apr 14 '21 at 06:33
  • it works!!!.. any probs importing the developer import ? – 68060 Dec 07 '21 at 12:10
  • @sisir Seems to log only work when the device is connected to the system to check the logs. As soon as you disconnect it will not show the log. Download APK and want to check the log of the app for testing purposes. Any help here? – Chintan Khetiya Dec 22 '22 at 12:48
70

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

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
23

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

debugPrint(someSuperLongString, wrapWidth: 1024);
jesobremonte
  • 3,198
  • 2
  • 22
  • 30
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));
       }
    }
}
CoderUni
  • 5,474
  • 7
  • 26
  • 58
Bisma Frühling
  • 776
  • 1
  • 11
  • 21
5

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);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ch271828n
  • 15,854
  • 5
  • 53
  • 88
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.

Mahi
  • 5,726
  • 13
  • 31
  • 41
3

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

debugPrint and print are actually truncating the output.

Tilo
  • 605
  • 7
  • 15
3

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.

Ilo Calistus
  • 2,005
  • 3
  • 19
  • 23
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

Anand
  • 4,355
  • 2
  • 35
  • 45
1

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

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

Sattar Hummatli
  • 1,360
  • 1
  • 15
  • 26
0

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)}');
leggod
  • 1
-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.

yug k
  • 349
  • 4
  • 9
-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.

Safeer
  • 1,407
  • 1
  • 25
  • 29