How to log data to the Flutter console?

假装没事ソ 提交于 2020-06-24 02:55:51

问题


Hello Flutter Devs :)

I am a beginner and using IntelliJ IDEA, and I wanted to log data to the console?

I tried print() and printDebug(), but none of my data were showing in the Flutter console.


回答1:


If you're inside a Flutter Widget, you can use debugPrint, e.g.,

import 'package:flutter/foundation.dart';

debugPrint('movieTitle: $movieTitle');


otherwise, you can use Dart's built in log function

import 'dart:developer';

log('data: $data');



回答2:


The Dart print() function outputs to the system console, which you can view using flutter logs (which is basically a wrapper around adb logcat).

If you output too much at once, then Android sometimes discards some log lines. To avoid this, you can use debugPrint().

Found here: https://flutter.io/docs/testing/debugging




回答3:


To be crystal clear, debugPrint will only work inside a Flutter widget.




回答4:


log() from 'dart:developer'

  • It doesn't seems to have maxlength limit like print() or debugPrint().

  • So it comes helpful when you want log whole api response.

  • And also helps in dart dev tools to show formatted logging.

import 'dart:developer';  //(auto import will do this even)
//example for api logging
  log("${response?.statusCode} :  ${response?.request?.path}",
          name: "Response", error: response.data);


来源:https://stackoverflow.com/questions/49940719/how-to-log-data-to-the-flutter-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!