从 Flutter 记录大字符串

我正在尝试建立一个扑动应用程序和学习飞镖的过程中,但我得到一种挫折时,调试。我已经从一个 API 中获取了一个资源,现在我想将 JSON 字符串打印到控制台,但是它总是切断这个字符串。

Screenshot of the cut off string in the console

所以我实际上有两个问题: 终端控制台真的是打印调试消息的唯一方法吗? 我怎样才能打印大字符串到控制台而不会自动切断它们?

55529 次浏览

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.

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

debugPrint and print are actually truncating the output.

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

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.

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

debugPrint(someSuperLongString, wrapWidth: 1024);

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

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]

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.

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

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.

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