为了避免deubg信息在生产环境打印,只在测试时打印,在main函数中,改变debugPrint的指向
main(){ if (Api.isDebug) { debugPrint = (String message, {int wrapWidth}) { try { var object = json.decode(message); message = JsonEncoder.withIndent(' ').convert(object); } catch (e) {} printWrapped(message); }; } else { debugPrint = (String message, {int wrapWidth}) {}; } }
将printWrapped方法放入工具类或你需要的地方
void printWrapped(String text) { final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk pattern.allMatches(text).forEach((match) => developer.log(match.group(0))); }
log方法需要引入
import 'dart:developer' as developer;