【问题标题】:Dart Annotation - Unable to pass non constant methodDart Annotation - Unable to pass non constant method
【发布时间】:2022-12-02 09:18:17
【问题描述】:

How can I pass-in non-constant method in Dart Annotations?

Here is my annotation:


class Injectable {
  final String name;
  final Scope scope;
  final List<Provider>? provider;

  const Injectable({this.name = '', this.scope = Scope.factory, this.provider});
}


class Provider<T> {
  final Type? useClass;
  final Object? useValue;
  final T? Function()? usefactory;
  final List<Type> deps;

 const Provider(
      {this.useClass, this.useValue, this.usefactory, this.deps = const []});
}

This works fine:

But when I try to directly pass-in the function. I am getting a compile error:

Any idea what is happening?

Error:


Arguments of a constant creation must be constant expressions. (Documentation)
Try making the argument a valid constant, or use 'new' to call the constructor.

Invalid constant value.

The argument type 'ServiceA' can't be assigned to the parameter type 'ServiceA? Function()?'. (Documentation)

【问题讨论】:

    标签: dart


    【解决方案1】:

    Please try to remove the brackets frominject<ServiceA>(). Just make itinject<ServiceA>.

    usefactory-s type is a function, but in your case, you are passingServiceAtype data.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2022-12-27
      • 2022-12-02
      • 2023-01-06
      • 2016-11-25
      相关资源
      最近更新 更多