gpt4 book ai didi

flutter - 如何读取注释字段及其值(代码生成)

转载 作者:行者123 更新时间:2023-12-03 02:56:21 26 4
gpt4 key购买 nike

我一直在努力从注释中获取字段。

我正在使用源代码生成器从我的注释生成我的小部件,但似乎无法使用注释字段,我如何访问字段和值。

我的注释:

class CoreEntity {
final String label;

const CoreEntity({
this.label,
});
}

使用注释:
@CoreEntity(
label: 'User',
)
class User {
String email;
}

我的小部件生成器:
class CoreWidgetGenerator extends GeneratorForAnnotation<CoreEntity> {
@override
FutureOr<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
return _generateWidgetSource(element);
}

String _generateWidgetSource(Element element) {
final anotData = element.metadata;
for (ElementAnnotation el in anotData) {
el.computeConstantValue();
var elVisitor = ModelVisitor();
el.element.visitChildren(elVisitor);
print("1 ${elVisitor.className}"); // this prints out null
print("2 ${elVisitor.fields}"); // this prints out empty list {}
}
}

class ModelVisitor extends SimpleElementVisitor {
DartType className;
Map<String, DartType> fields = Map();

@override
visitConstructorElement(ConstructorElement element) {
className = element.type.returnType;
return super.visitConstructorElement(element);
}

@override
visitFieldElement(FieldElement element) {
fields[element.name] = element.type;

return super.visitFieldElement(element);
}
}

pubspec.yaml 中的包
dependencies:
build: ^1.1.6
source_gen: ^0.9.4+4
core_widget:
path: ../core_widgets

构建脚本(build.yaml)
targets:
$default:
builders:
core_gen|core_widget:
enabled: true

builders:
core_widget:
target: ":core_gen"
import: "package:core_gen/builder.dart"
builder_factories: ["coreWidget"]
build_extensions: {".dart":[".core_widget.g.part"]}
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]

引用资料|我做过但没有运气的研究。
  • The code is built of this tutorial
  • Similar Question
  • 最佳答案

    您已经可以通过 ConstantReader 访问注释属性

    class CoreWidgetGenerator extends GeneratorForAnnotation<CoreEntity> {
    @override
    FutureOr<String> generateForAnnotatedElement(
    Element element, ConstantReader annotation, BuildStep buildStep) {
    return _generateWidgetSource(element, annotation);
    }

    String _generateWidgetSource(Element element, ConstantReader annotation) {
    print(annotation.read('label').stringValue);
    }
    }

    但是,如果要检索 ClassElement 字段的注释信息,可以使用 TypeChecker
    注解

    class CoreProperty {
    final String label;

    const CoreProperty({ this.label});
    }

    用法

    @CoreEntity(label: 'User')
    class User {

    @CoreProperty(label: 'Email')
    String email;
    }

    final _coreChecker = const TypeChecker.fromRuntime(CoreProperty);

    class CoreWidgetGenerator extends GeneratorForAnnotation<CoreEntity> {
    @override
    FutureOr<String> generateForAnnotatedElement(
    Element element, ConstantReader annotation, BuildStep buildStep) {
    return _generateWidgetSource(element as ClassElement, annotation);
    }

    String _generateWidgetSource(ClassElement element, ConstantReader annotation) {

    for (var f in element.fields) {
    if (_coreChecker.hasAnnotationOfExact(f)) {
    print(_coreChecker
    .firstAnnotationOfExact(f)
    .getField('label')
    .toStringValue()
    );
    }
    }
    }
    }

    关于flutter - 如何读取注释字段及其值(代码生成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57703473/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com