Flutter常用工具

一、 JSON打印格式化

class JsonUtil {
 //带有首行缩进的Json格式
 static JsonEncoder encoder = JsonEncoder.withIndent('  ');

 /// 单纯的Json格式输出打印
 static void printJson(Object object) {
   try {
     var encoderString = encoder.convert(object);
     debugPrint(encoderString);
   } catch (e) {
     print(e);
   }
 }

 /// 接收Dio请求库返回的Response对象
 static void printRespond(HiNetResponse response) {
   Map httpLogMap = Map();
   httpLogMap.putIfAbsent("requestUrl", () => "${response.request.url()}");
   httpLogMap.putIfAbsent("requestHeaders", () => response.request.header);
   httpLogMap.putIfAbsent(
       "requestQueryParameters", () => response.request.params);
   httpLogMap.putIfAbsent("respondData", () => response.data);

   printJson(httpLogMap);
 }
}

二、埋点信息需要的设备信息采集

https://pub.flutter-io.cn/packages/device_info

import 'dart:io';
import 'package:device_info/device_info.dart';

void getDeviceInfo() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  if (Platform.isIOS) {
    IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
  } else if (Platform.isAndroid) {
    AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
  }
}
image.png

你可能感兴趣的:(Flutter常用工具)