![](/img/trans.png)
[英]How can I output my object properties with this enumerating extension in Flutter/Dart?
[英]How can I apply dart extension methods in the Flutter Logging library?
我正在嘗試在我的 flutter 項目中記錄庫。 他們有一些很好的記錄消息(例如 record.name,record.message 等)對於我的項目,我想通過使用 dart擴展方法擴展這個 package 添加更多記錄消息(例如 record.version,record.eventName, ETC。)。 作為dart的初學者,我完全不知道該怎么做?
這是我嘗試但失敗的示例。
extension CustomLog on LogRecord {
String version(LogRecord version) => "1.0";
String eventName(LogRecord eventName) => "userNameChangedEvent";
}
請給我一些建議/示例,我如何擴展任何 package 並自己使用它。
日志記錄不支持擴展(帶有擴展名)LogRecord 以添加自定義字段。
您可以將所有自定義信息作為 class object 傳遞給object
參數,稍后再檢索。
就像是
class CustomLogAttributes {
final String version;
final String eventName;
CustomLogAttributes(this.version, this.eventName);
}
然后
log.fine("示例日志消息", object: CustomLogAttributes("1.1.0", "example_event");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.