You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter 3.10.1中dart:developer包的log函数失效求解决方案

Flutter 3.10.1中dart:developer log函数失效的替代方案

方案1:使用print拼接长字符串

直接将所有日志内容拼接为单个字符串后调用print,确保控制台输出单条日志,性能和原log函数接近。

// 处理多行或复杂内容
String generateLogContent() {
  final buffer = StringBuffer();
  buffer.writeln("用户操作日志");
  buffer.writeln("操作时间: ${DateTime.now()}");
  buffer.writeln("操作详情: 提交表单,数据: ${userForm.toJson()}");
  return buffer.toString();
}

// 打印单条日志
print(generateLogContent());

方案2:配置debugPrint禁用自动拆分

debugPrint默认会拆分长日志,通过设置wrapWidth为极大值,强制单条输出,且仅在debug模式生效(避免release模式冗余日志)。

import 'package:flutter/foundation.dart';

void singleLineDebugLog(String message) {
  debugPrint(
    message,
    wrapWidth: int.maxFinite, // 禁用换行拆分
  );
}

// 使用示例
singleLineDebugLog("这是一条很长的日志内容,包含多个字段和详细信息,不会被拆分成多条控制台输出");

方案3:平台原生日志API(跨模式可用)

通过MethodChannel调用Android/iOS的原生日志接口,确保日志以单条形式输出,且在release模式也能正常工作。

Dart层代码

import 'package:flutter/services.dart';

class NativeLog {
  static const _channel = MethodChannel('com.example/native_log');

  static void d(String tag, String message) async {
    try {
      await _channel.invokeMethod('debugLog', {
        'tag': tag,
        'message': message,
      });
    } catch (e) {
      print("Native log failed: $e");
    }
  }
}

// 使用示例
NativeLog.d("FormSubmit", "用户提交表单,数据: ${formData.toString()}");

Android原生层(Kotlin)

在MainActivity.kt中添加MethodChannel处理:

import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.example/native_log"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            when (call.method) {
                "debugLog" -> {
                    val tag = call.argument<String>("tag") ?: "FlutterNativeLog"
                    val message = call.argument<String>("message") ?: ""
                    Log.d(tag, message)
                    result.success(null)
                }
                else -> result.notImplemented()
            }
        }
    }
}

iOS原生层(Swift)

在AppDelegate.swift中添加MethodChannel处理:

import UIKit
import Flutter
import os.log

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel(name: "com.example/native_log", binaryMessenger: controller.binaryMessenger)
        channel.setMethodCallHandler { (call, result) in
            if call.method == "debugLog" {
                let tag = call.arguments!["tag"] as? String ?? "FlutterNativeLog"
                let message = call.arguments!["message"] as? String ?? ""
                os_log("%@", log: OSLog(subsystem: tag, category: "default"), type: .debug, message)
                result(nil)
            } else {
                result(FlutterMethodNotImplemented)
            }
        }
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

内容的提问来源于stack exchange,提问作者devmuaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:23:20