[英]IntelliJ Live Template for dart named constructor: lists class' fields
I want to generate a custom named constructor in Dart.我想在 Dart 中生成一个自定义命名的构造函数。 I have many dto class to implement and each should provide a named constructor like: ClassName.fromMap()
.我有很多 dto 类要实现,每个类都应该提供一个命名构造函数,如: ClassName.fromMap()
。
For example for this class:例如对于这个类:
class Student {
final String name;
final int age;
}
The generated constructor should be:生成的构造函数应该是:
Student.fromMap(Map<String, dynamic> map) :
name = map['name'],
age = map['age'];
How can I retrieve the list of the field of my current class as strings?如何以字符串形式检索当前类的字段列表? Is that even possibile?这甚至可能吗?
Of course I can have a variable number of fields.当然,我可以拥有可变数量的字段。
My template looks like:我的模板看起来像:
$CLASS$.fromMap(Map<String, dynamic> map) :
$INITIALIZATION_LIST$
binding $CLASS$
to dartClassName()
.将$CLASS$
绑定到dartClassName()
。
Now I'd like to bind $INITIALIZATION_LIST$
to something like:现在我想将$INITIALIZATION_LIST$
绑定到以下内容:
getClassFieldList().forEach((fieldName) => "$fieldName = map['$fieldName']")
Can I achieve something like that?我可以实现这样的目标吗?
There is no way to retrieve a list of Dart class fields using predefined live template functions.无法使用预定义的实时模板函数检索 Dart 类字段列表。 You can try developing your own template macro for this.您可以尝试为此开发自己的模板宏。 See https://intellij-support.jetbrains.com/hc/en-us/community/posts/206201699-create-a-new-expression-for-a-live-template-for-actionscript for some hints.有关一些提示,请参阅https://intellij-support.jetbrains.com/hc/en-us/community/posts/206201699-create-a-new-expression-for-a-live-template-for-actionscript 。 Existing live template functions implementations can be found at https://github.com/JetBrains/intellij-community/tree/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro .现有的实时模板函数实现可以在https://github.com/JetBrains/intellij-community/tree/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro找到。
You can also try using Structural Search and Replace instead of live template您也可以尝试使用结构搜索和替换代替实时模板
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.