android-studio - dart 命名构造函数的 IntelliJ 实时模板:列出类的字段
问题描述
我想在 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找到现有的实时模板函数实现。
您也可以尝试使用结构搜索和替换而不是实时模板
推荐阅读
- javascript - 什么情况下会静音
- asp.net - 当 Web.config 元素已经包含不同的 xmlns 时如何使用 xdt-transform
- responsive - 为什么我的网站面临响应问题?
- javascript - 用JS操作元素的css动画位置
- python - 作为 python 包维护者,我如何确定最低工作要求
- python - 将字符位置乘以该字符的 ord() 值
- amazon-web-services - 暂停多个 Auto Scaling 组的 ASG 进程
- oracle - ORDS:错误 404 - DispatcherNotFoundException
- python - 如何在 Azure Python SDK 中检查资源创建是否成功
- html - 如何以特定方式定位此元素