visitProvideClass method

  1. @override
void visitProvideClass(
  1. int index,
  2. TokenElement token,
  3. Expression tokenExpression,
  4. Reference type,
  5. String? constructor,
  6. List<Expression> dependencies,
  7. bool isMulti,
)
override

Implement providing a new instance of type, calling constructor.

Any dependencies are expected to invoke local methods as appropriate:

refer('inject').call([refer('Dep1')])

Implementation

@override
void visitProvideClass(
  int index,
  TokenElement token,
  Expression tokenExpression,
  Reference type,
  String? constructor,
  List<Expression> dependencies,
  bool isMulti,
) {
  final fieldName = '_field$index';
  final types = type is TypeReference ? type.types : <Reference>[];
  _fieldCache.add(
    Field((b) => b
      ..name = fieldName
      ..type = TypeReference((b) => b
        ..symbol = type.symbol
        ..url = type.url
        ..types.addAll(types)
        ..isNullable = true)),
  );

  final methodName = '_get${type.symbol}\$$index';
  final instance = constructor == null
      ? type.newInstance(dependencies)
      : type.newInstanceNamed(constructor, dependencies);
  _methodCache.add(Method((b) => b
    ..name = methodName
    ..returns = type
    ..body = refer(fieldName).assignNullAware(instance).code));

  if (isMulti) {
    _addToMulti(token, tokenExpression, methodName);
  } else {
    _addToBody(tokenExpression, methodName);
  }
}