[英]IntelliJ Live Template for dart named constructor: lists class' fields
我想在 Dart 中生成一個自定義命名的構造函數。 我有很多 dto 類要實現,每個類都應該提供一個命名構造函數,如: ClassName.fromMap()
。
例如對於這個類:
class Student {
final String name;
final int age;
}
生成的構造函數應該是:
Student.fromMap(Map<String, dynamic> map) :
name = map['name'],
age = map['age'];
如何以字符串形式檢索當前類的字段列表? 這甚至可能嗎?
當然,我可以擁有可變數量的字段。
我的模板看起來像:
$CLASS$.fromMap(Map<String, dynamic> map) :
$INITIALIZATION_LIST$
將$CLASS$
綁定到dartClassName()
。
現在我想將$INITIALIZATION_LIST$
綁定到以下內容:
getClassFieldList().forEach((fieldName) => "$fieldName = map['$fieldName']")
我可以實現這樣的目標嗎?
無法使用預定義的實時模板函數檢索 Dart 類字段列表。 您可以嘗試為此開發自己的模板宏。 有關一些提示,請參閱https://intellij-support.jetbrains.com/hc/en-us/community/posts/206201699-create-a-new-expression-for-a-live-template-for-actionscript 。 現有的實時模板函數實現可以在https://github.com/JetBrains/intellij-community/tree/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro找到。
您也可以嘗試使用結構搜索和替換代替實時模板
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.