0

我正在按照raywenderlich.combuild_runner上的示例代码为颤振应用程序编写飞镖代码生成器。在遍历期间,我想访问下面示例代码中的参数barKey

@myGenerator
class Blah extends StatelessWidget {
  Key fooKey;
  @override
  Widget build(BuildContext context) {
    return Text('Blah blah', key: barKey);
  }
}

我的访客扩展visitFieldElementvisitMethodElement

class ModelVisitor extends SimpleElementVisitor<dynamic> {
  String className;
  List<String> data = <String>[];
  @override
  dynamic visitConstructorElement(ConstructorElement element) {
    final elementReturnType = element.type.returnType.toString();
    className = elementReturnType.replaceFirst('*', '');
  }
  @override
  dynamic visitFieldElement(FieldElement element) {
    final elementType = element.type.toString();
    data.add('hello from visitFieldElement');
    // DartType ends with '*', which needs to be eliminated
    // for the generated code to be accurate.
    data.add('name: ${element.name}');
    data.add('type: ${elementType}');
  }
  @override
  dynamic visitMethodElement(MethodElement element) {
    data.add('hello from visitMethodElement');
    data.add('name: ${element.name}');
    element.visitChildren(this);
  }
}

从输出中可以看出,fooKey成员被访问但不是barKey传递的参数?

// hello from visitFieldElement
// name: fooKey
// type: Key*
// hello from visitMethodElement
// name: build

我将如何访问barKey

编辑:我在标题中发现了这条评论element.dart

/// The element model does not contain everything in the code, only those things
/// that are declared by the code. For example, it does not include any
/// representation of the statements in a method body, but if one of those
/// statements declares a local variable then the local variable will be
/// represented by an element.

所以,看起来没有办法在代码生成期间使用 build_runner 访问方法体:/

4

0 回答 0