visitProvideFactory method

  1. @override
void visitProvideFactory(
  1. int index,
  2. TokenElement token,
  3. Expression tokenExpression,
  4. Reference returnType,
  5. Reference function,
  6. List<Expression> dependencies,
  7. bool isMulti,
)
override

Implement providing token by calling function.

Any dependencies are expected to invoke local methods as appropriate:

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

Implementation

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

  final methodName = '_get${returnType.symbol}\$$index';
  _methodCache.add(
    Method(
      (b) => b
        ..name = methodName
        ..returns = returnType
        ..body = refer(fieldName)
            .assignNullAware(function.call(dependencies))
            .code,
    ),
  );

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