簡體   English   中英

如何在 Flutter 中禁用發布版本中的所有日志 [debugPrint()]?

[英]How to disable all Logs [debugPrint()] in release build in flutter?

我已經在 android 設備中安裝了 release build apk,但是如果我將該設備連接到 Android studio,那么我可以看到所有 Logs/debugPrint 語句。

有什么辦法可以禁用所有日志?

我將接受的答案與來自這里的想法結合起來,並使用它 main.dart 使所有 debugPrint 無處不在。

const bool isProduction = bool.fromEnvironment('dart.vm.product');
void main() {
  if (isProduction) {      
      // analyser does not like empty function body
      // debugPrint = (String message, {int wrapWidth}) {};
      // so i changed it to this:
      debugPrint = (String? message, {int? wrapWidth}) => null;

  } 
  runApp(
    MyApp()
  );
}

您可以為全局debugPrint變量分配一個虛擬函數:

import 'package:flutter/material.dart';

main() {
  debugPrint = (String message, {int wrapWidth}) {};
}

使用最新的 Flutter 版本返回空函數不起作用。 不建議並且應該避免返回 null - 如果使用,它會標有 void 函數不應返回 null 的標記。 相反,空字符串不會導致任何問題並且可以完美運行:

main() {
  debugPrint = (String? message, {int? wrapWidth}) => '';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM