google / reflectable.dart

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.

Home Page:https://pub.dev/packages/reflectable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug?] ERROR: This requires the 'constructor-tearoffs' language feature to be enabled.

dinbtechit opened this issue · comments

Hi, Unable to parse the constructor with "Function with generics". Is it a bug? Or am I doing something wrong?

My Reflectable implementation

/// The Inject Method, the provider is trying to call in the below Provider constructor -> usefactory.
T inject<T extends Object>({String? byName}) {
  if (byName != null) {
    return DependencyInjection.objectsForDI.entries
        .toList()
        .firstWhere(
            (element) => element.key.toLowerCase() == byName.toLowerCase())
        .value as T;
  }

  return DependencyInjection.objectsForDI.entries
      .toList()
      .firstWhere((element) => element.value is T)
      .value as T;
}


/// Annotation Usage
/// Injectable annotation is defined in another file.

@Injectable(
  name: 'prod-ApiService',
  scope: Scope.lazySingleton,
  provider: [
    Provider<ServiceA>(useValue: inject<ServiceA>)   <----<<  // Fails to parse this line.
  ],
)
class ApiServiceProd implements ApiServiceIF {
  @override
  handle() {
    print('This is a prod service');
  }
}

Full Error:

╰$ flutter pub run build_runner build

[INFO] Generating build script...
[INFO] Generating build script completed, took 157ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 26ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 429ms

[INFO] Running build...
[INFO] 1.0s elapsed, 0/2 actions completed.
[INFO] 5.2s elapsed, 0/2 actions completed.
[INFO] 6.3s elapsed, 0/2 actions completed.
[INFO] 7.3s elapsed, 0/2 actions completed.
[INFO] 8.4s elapsed, 0/2 actions completed.
[SEVERE] reflectable on lib/main.dart:

Could not format because the source could not be parsed:

line 26, column 2236 of .: This requires the 'constructor-tearoffs' language feature to be enabled.
   ╷
26 │ final _data = <r.Reflectable, r.ReflectorData>{const prefix0.Reflector(): r.ReflectorData(<m.TypeMirror>[r.NonGenericClassMirrorImpl(r'Widget1', r'.Widget1', 134217735, 0, const prefix0.Reflector(), const <int>[0, 1, 2, 6, 10], const <int>[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 6, 7, 8, 9], const <int>[], -1, {}, {}, {r'': (bool b) => ({serviceA, serviceB, serviceC, key}) => b ? prefix1.Widget1(key: key, serviceA: serviceA, serviceB: serviceB, serviceC: serviceC) : null}, 0, 0, const <int>[], const <Object>[const prefix0.Component(provider: const [const prefix0.Provider<ApiServiceIF>(usefactory: prefix1.myFactory)])], null), r.NonGenericClassMirrorImpl(r'Widget2', r'.Widget2', 134217735, 1, const prefix0.Reflector(), const <int>[4, 5, 24, 27], const <int>[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 28, 24, 25, 26], const <int>[], -1, {}, {}, {r'': (bool b) => ({key, serviceA, serviceB}) => b ? prefix1.Widget2(key: key, serviceA: serviceA, serviceB: serviceB) : null}, 0, 1, const <int>[], const <Object>[const prefix0.Component(provider: const [const prefix0.Provider<ApiServiceIF>(useClass: prefix2.ApiServiceProd)])], null), r.NonGenericClassMirrorImpl(r'ApiServiceIF', r'.ApiServiceIF', 134218247, 2, const prefix0.Reflector(), const <int>[29, 30], const <int>[31, 32, 13, 33, 15], const <int>[], -1, {}, {}, {}, 1, 2, const <int>[], const <Object>[const prefix0.Injectable()], null), r.NonGenericClassMirrorImpl(r'ApiService', r'.ApiService', 134217735, 3, const prefix0.Reflector(), const <int>[34, 35], const <int>[31, 32, 13, 33, 15, 34], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix2.ApiService() : null}, 1, 3, const <int>[2], const <Object>[const prefix0.Injectable(name: 'local-ApiService', scope: prefix0.Scope.singleton)], null), r.NonGenericClassMirrorImpl(r'ApiServiceProd', r'.ApiServiceProd', 134217735, 4, const prefix0.Reflector(), const <int>[36, 37], const <int>[31, 32, 13, 33, 15, 36], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix2.ApiServiceProd() : null}, 1, 4, const <int>[2], const <Object>[const prefix0.Injectable(name: 'prod-ApiService', scope: prefix0.Scope.lazySingleton, provider: const [const prefix0.Provider<ServiceA>(useValue: inject<ServiceA>)])], null), r.NonGenericClassMirrorImpl(r'ServiceA', r'.ServiceA', 134217735, 5, const prefix0.Reflector(), const <int>[38, 39, 40], const <int>[31, 32, 13, 33, 15, 38, 39], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix3.ServiceA() : null}, 2, 5, const <int>[], const <Object>[const prefix0.Injectable(name: 'ServiceA', provider: const [const prefix0.Provider(useClass: prefix3.ServiceB)])], null), r.NonGenericClassMirrorImpl(r'ServiceB', r'.ServiceB', 134217735, 6, const prefix0.Reflector(), const <int>[41, 42], const <int>[31, 32, 13, 33, 15, 41], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix3.ServiceB() : null}, 2, 6, const <int>[], const <Object>[const prefix0.Injectable(name: 'ServiceB')], null), r.NonGenericClassMirrorImpl(r'ServiceC', r'.ServiceC', 134217735, 7, const prefix0.Reflector(), const <int>[43, 44], const <int>[31, 32, 13, 33, 15, 43], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix3.ServiceC() : null}, 2, 7, const <int>[], const <Object>[const prefix0.Injectable(name: 'ServiceC')], null)], <m.DeclarationMirror>[r.VariableMirrorImpl(r'serviceA', 134349829, 0, const prefix0.Reflector(), 5, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceB', 134349829, 0, const prefix0.Reflector(), 6, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceC', 134349829, 0, const prefix0.Reflector(), 7, -1, -1, null, const []), r.VariableMirrorImpl(r'key', 67240965, -1, const prefix0.Reflector(), -1, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceA', 134349829, 1, const prefix0.Reflector(), 5, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceB', 134349829, 1, const prefix0.Reflector(), 6, -1, -1, null, const []), r.MethodMirrorImpl(r'build', 2097154, 0, -1, -1, -1, null, const <int>[0], const prefix0.Reflector(), const <Object>[override]), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 0, 7), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 1, 8), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 2, 9), r.MethodMirrorImpl(r'', 128, 0, -1, -1, -1, null, const <int>[1, 2, 3, 4], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'==', 2097154, -1, -1, -1, -1, null, const <int>[5], const prefix0.Reflector(), const <Object>[override, prefix4.nonVirtual]), r.MethodMirrorImpl(r'toString', 2097154, -1, -1, -1, -1, null, const <int>[6], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'noSuchMethod', 524290, -1, -1, -1, -1, null, const <int>[7], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'hashCode', 2097155, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override, prefix4.nonVirtual]), r.MethodMirrorImpl(r'runtimeType', 2097155, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'toStringShort', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'toDiagnosticsNode', 2097154, -1, -1, -1, -1, null, const <int>[8, 9], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'debugFillProperties', 1310722, -1, -1, -1, -1, null, const <int>[10], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'toStringShallow', 2097154, -1, -1, -1, -1, null, const <int>[11, 12], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'toStringDeep', 2097154, -1, -1, -1, -1, null, const <int>[13, 14, 15], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'debugDescribeChildren', 35651586, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[prefix4.protected]), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 3, 22), r.MethodMirrorImpl(r'createElement', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'createState', 35651586, 1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 4, 25), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 5, 26), r.MethodMirrorImpl(r'', 128, 1, -1, -1, -1, null, const <int>[16, 17, 18], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'createElement', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'handle', 1311234, 2, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 64, 2, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'==', 2097154, -1, -1, -1, -1, null, const <int>[19], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'toString', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'hashCode', 2097155, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'handle', 1310722, 3, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'', 64, 3, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'handle', 1310722, 4, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'', 64, 4, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getText', 2097154, 5, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getCount', 2097154, 5, -1, -1, -1, null, const <int>[20], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 128, 5, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getText', 2097154, 6, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 64, 6, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getText', 2097154, 7, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 64, 7, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const [])], <m.ParameterMirror>[r.ParameterMirrorImpl(r'context', 134348806, 6, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'serviceA', 134358022, 10, const prefix0.Reflector(), 5, -1, -1, null, const [], null, #serviceA), r.ParameterMirrorImpl(r'serviceB', 134358022, 10, const prefix0.Reflector(), 6, -1, -1, null, const [], null, #serviceB), r.ParameterMirrorImpl(r'serviceC', 134358022, 10, const prefix0.Reflector(), 7, -1, -1, null, const [], null, #serviceC), r.ParameterMirrorImpl(r'key', 67253254, 10, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #key), r.ParameterMirrorImpl(r'other', 134348806, 11, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'minLevel', 134363142, 12, const prefix0.Reflector(), -1, -1, -1, null, const [], prefix5.DiagnosticLevel.info, #minLevel), r.ParameterMirrorImpl(r'invocation', 134348806, 13, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'name', 67252230, 17, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #name), r.ParameterMirrorImpl(r'style', 67252230, 17, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #style), r.ParameterMirrorImpl(r'properties', 134348806, 18, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'joiner', 134363142, 19, const prefix0.Reflector(), -1, -1, -1, null, const [], ', ', #joiner), r.ParameterMirrorImpl(r'minLevel', 134363142, 19, const prefix0.Reflector(), -1, -1, -1, null, const [], prefix5.DiagnosticLevel.debug, #minLevel), r.ParameterMirrorImpl(r'prefixLineOne', 134363142, 20, const prefix0.Reflector(), -1, -1, -1, null, const [], '', #prefixLineOne), r.ParameterMirrorImpl(r'prefixOtherLines', 67252230, 20, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #prefixOtherLines), r.ParameterMirrorImpl(r'minLevel', 134363142, 20, const prefix0.Reflector(), -1, -1, -1, null, const [], prefix5.DiagnosticLevel.debug, #minLevel), r.ParameterMirrorImpl(r'key', 67252230, 27, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #key), r.ParameterMirrorImpl(r'serviceA', 134358022, 27, const prefix0.Reflector(), 5, -1, -1, null, const [], null, #serviceA), r.ParameterMirrorImpl(r'serviceB', 134358022, 27, const prefix0.Reflector(), 6, -1, -1, null, const [], null, #serviceB), r.ParameterMirrorImpl(r'other', 134348806, 31, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'i', 134348806, 39, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null)], <Type>[prefix1.Widget1, prefix1.Widget2, prefix2.ApiServiceIF, prefix2.ApiService, prefix2.ApiServiceProd, prefix3.ServiceA, prefix3.ServiceB, prefix3.ServiceC], 8, {r'==': (dynamic instance) => (x) => instance == x, r'toString': (dynamic instance) => instance.toString, r'noSuchMethod': (dynamic instance) => instance.noSuchMethod, r'hashCode': (dynamic instance) => instance.hashCode, r'runtimeType': (dynamic instance) => instance.runtimeType, r'toStringShort': (dynamic instance) => instance.toStringShort, r'toDiagnosticsNode': (dynamic instance) => instance.toDiagnosticsNode, r'debugFillProperties': (dynamic instance) => instance.debugFillProperties, r'toStringShallow': (dynamic instance) => instance.toStringShallow, r'toStringDeep': (dynamic instance) => instance.toStringDeep, r'debugDescribeChildren': (dynamic instance) => instance.debugDescribeChildren, r'key': (dynamic instance) => instance.key, r'createElement': (dynamic instance) => instance.createElement, r'build': (dynamic instance) => instance.build, r'serviceA': (dynamic instance) => instance.serviceA, r'serviceB': (dynamic instance) => instance.serviceB, r'serviceC': (dynamic instance) => instance.serviceC, r'createState': (dynamic instance) => instance.createState, r'handle': (dynamic instance) => instance.handle, r'getText': (dynamic instance) => instance.getText, r'getCount': (dynamic instance) => instance.getCount}, {}, <m.LibraryMirror>[r.LibraryMirrorImpl(r'', Uri.parse(r'reflectable://0/'), const prefix0.Reflector(), const <int>[], {}, {}, const [], null), r.LibraryMirrorImpl(r'', Uri.parse(r'reflectable://1/'), const prefix0.Reflector(), const <int>[], {}, {}, const [], null), r.LibraryMirrorImpl(r'', Uri.parse(r'reflectable://2/'), const prefix0.Reflector(), const <int>[], {}, {}, const [], null)], [])};
   │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ^^^^^^^^^^
   ╵
[SEVERE] reflectable on test/widget_test.dart:

Could not format because the source could not be parsed:

line 26, column 2236 of .: This requires the 'constructor-tearoffs' language feature to be enabled.
   ╷
26 │ final _data = <r.Reflectable, r.ReflectorData>{const prefix0.Reflector(): r.ReflectorData(<m.TypeMirror>[r.NonGenericClassMirrorImpl(r'Widget1', r'.Widget1', 134217735, 0, const prefix0.Reflector(), const <int>[0, 1, 2, 6, 10], const <int>[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 6, 7, 8, 9], const <int>[], -1, {}, {}, {r'': (bool b) => ({serviceA, serviceB, serviceC, key}) => b ? prefix1.Widget1(key: key, serviceA: serviceA, serviceB: serviceB, serviceC: serviceC) : null}, 0, 0, const <int>[], const <Object>[const prefix0.Component(provider: const [const prefix0.Provider<ApiServiceIF>(usefactory: prefix1.myFactory)])], null), r.NonGenericClassMirrorImpl(r'Widget2', r'.Widget2', 134217735, 1, const prefix0.Reflector(), const <int>[4, 5, 24, 27], const <int>[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 28, 24, 25, 26], const <int>[], -1, {}, {}, {r'': (bool b) => ({key, serviceA, serviceB}) => b ? prefix1.Widget2(key: key, serviceA: serviceA, serviceB: serviceB) : null}, 0, 1, const <int>[], const <Object>[const prefix0.Component(provider: const [const prefix0.Provider<ApiServiceIF>(useClass: prefix2.ApiServiceProd)])], null), r.NonGenericClassMirrorImpl(r'ApiServiceIF', r'.ApiServiceIF', 134218247, 2, const prefix0.Reflector(), const <int>[29, 30], const <int>[31, 32, 13, 33, 15], const <int>[], -1, {}, {}, {}, 1, 2, const <int>[], const <Object>[const prefix0.Injectable()], null), r.NonGenericClassMirrorImpl(r'ApiService', r'.ApiService', 134217735, 3, const prefix0.Reflector(), const <int>[34, 35], const <int>[31, 32, 13, 33, 15, 34], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix2.ApiService() : null}, 1, 3, const <int>[2], const <Object>[const prefix0.Injectable(name: 'local-ApiService', scope: prefix0.Scope.singleton)], null), r.NonGenericClassMirrorImpl(r'ApiServiceProd', r'.ApiServiceProd', 134217735, 4, const prefix0.Reflector(), const <int>[36, 37], const <int>[31, 32, 13, 33, 15, 36], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix2.ApiServiceProd() : null}, 1, 4, const <int>[2], const <Object>[const prefix0.Injectable(name: 'prod-ApiService', scope: prefix0.Scope.lazySingleton, provider: const [const prefix0.Provider<ServiceA>(useValue: inject<ServiceA>)])], null), r.NonGenericClassMirrorImpl(r'ServiceA', r'.ServiceA', 134217735, 5, const prefix0.Reflector(), const <int>[38, 39, 40], const <int>[31, 32, 13, 33, 15, 38, 39], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix3.ServiceA() : null}, 2, 5, const <int>[], const <Object>[const prefix0.Injectable(name: 'ServiceA', provider: const [const prefix0.Provider(useClass: prefix3.ServiceB)])], null), r.NonGenericClassMirrorImpl(r'ServiceB', r'.ServiceB', 134217735, 6, const prefix0.Reflector(), const <int>[41, 42], const <int>[31, 32, 13, 33, 15, 41], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix3.ServiceB() : null}, 2, 6, const <int>[], const <Object>[const prefix0.Injectable(name: 'ServiceB')], null), r.NonGenericClassMirrorImpl(r'ServiceC', r'.ServiceC', 134217735, 7, const prefix0.Reflector(), const <int>[43, 44], const <int>[31, 32, 13, 33, 15, 43], const <int>[], -1, {}, {}, {r'': (bool b) => () => b ? prefix3.ServiceC() : null}, 2, 7, const <int>[], const <Object>[const prefix0.Injectable(name: 'ServiceC')], null)], <m.DeclarationMirror>[r.VariableMirrorImpl(r'serviceA', 134349829, 0, const prefix0.Reflector(), 5, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceB', 134349829, 0, const prefix0.Reflector(), 6, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceC', 134349829, 0, const prefix0.Reflector(), 7, -1, -1, null, const []), r.VariableMirrorImpl(r'key', 67240965, -1, const prefix0.Reflector(), -1, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceA', 134349829, 1, const prefix0.Reflector(), 5, -1, -1, null, const []), r.VariableMirrorImpl(r'serviceB', 134349829, 1, const prefix0.Reflector(), 6, -1, -1, null, const []), r.MethodMirrorImpl(r'build', 2097154, 0, -1, -1, -1, null, const <int>[0], const prefix0.Reflector(), const <Object>[override]), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 0, 7), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 1, 8), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 2, 9), r.MethodMirrorImpl(r'', 128, 0, -1, -1, -1, null, const <int>[1, 2, 3, 4], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'==', 2097154, -1, -1, -1, -1, null, const <int>[5], const prefix0.Reflector(), const <Object>[override, prefix4.nonVirtual]), r.MethodMirrorImpl(r'toString', 2097154, -1, -1, -1, -1, null, const <int>[6], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'noSuchMethod', 524290, -1, -1, -1, -1, null, const <int>[7], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'hashCode', 2097155, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override, prefix4.nonVirtual]), r.MethodMirrorImpl(r'runtimeType', 2097155, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'toStringShort', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'toDiagnosticsNode', 2097154, -1, -1, -1, -1, null, const <int>[8, 9], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'debugFillProperties', 1310722, -1, -1, -1, -1, null, const <int>[10], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'toStringShallow', 2097154, -1, -1, -1, -1, null, const <int>[11, 12], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'toStringDeep', 2097154, -1, -1, -1, -1, null, const <int>[13, 14, 15], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'debugDescribeChildren', 35651586, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[prefix4.protected]), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 3, 22), r.MethodMirrorImpl(r'createElement', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'createState', 35651586, 1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 4, 25), r.ImplicitGetterMirrorImpl(const prefix0.Reflector(), 5, 26), r.MethodMirrorImpl(r'', 128, 1, -1, -1, -1, null, const <int>[16, 17, 18], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'createElement', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'handle', 1311234, 2, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 64, 2, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'==', 2097154, -1, -1, -1, -1, null, const <int>[19], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'toString', 2097154, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'hashCode', 2097155, -1, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'handle', 1310722, 3, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'', 64, 3, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'handle', 1310722, 4, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const <Object>[override]), r.MethodMirrorImpl(r'', 64, 4, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getText', 2097154, 5, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getCount', 2097154, 5, -1, -1, -1, null, const <int>[20], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 128, 5, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getText', 2097154, 6, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 64, 6, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'getText', 2097154, 7, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const []), r.MethodMirrorImpl(r'', 64, 7, -1, -1, -1, null, const <int>[], const prefix0.Reflector(), const [])], <m.ParameterMirror>[r.ParameterMirrorImpl(r'context', 134348806, 6, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'serviceA', 134358022, 10, const prefix0.Reflector(), 5, -1, -1, null, const [], null, #serviceA), r.ParameterMirrorImpl(r'serviceB', 134358022, 10, const prefix0.Reflector(), 6, -1, -1, null, const [], null, #serviceB), r.ParameterMirrorImpl(r'serviceC', 134358022, 10, const prefix0.Reflector(), 7, -1, -1, null, const [], null, #serviceC), r.ParameterMirrorImpl(r'key', 67253254, 10, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #key), r.ParameterMirrorImpl(r'other', 134348806, 11, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'minLevel', 134363142, 12, const prefix0.Reflector(), -1, -1, -1, null, const [], prefix5.DiagnosticLevel.info, #minLevel), r.ParameterMirrorImpl(r'invocation', 134348806, 13, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'name', 67252230, 17, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #name), r.ParameterMirrorImpl(r'style', 67252230, 17, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #style), r.ParameterMirrorImpl(r'properties', 134348806, 18, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'joiner', 134363142, 19, const prefix0.Reflector(), -1, -1, -1, null, const [], ', ', #joiner), r.ParameterMirrorImpl(r'minLevel', 134363142, 19, const prefix0.Reflector(), -1, -1, -1, null, const [], prefix5.DiagnosticLevel.debug, #minLevel), r.ParameterMirrorImpl(r'prefixLineOne', 134363142, 20, const prefix0.Reflector(), -1, -1, -1, null, const [], '', #prefixLineOne), r.ParameterMirrorImpl(r'prefixOtherLines', 67252230, 20, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #prefixOtherLines), r.ParameterMirrorImpl(r'minLevel', 134363142, 20, const prefix0.Reflector(), -1, -1, -1, null, const [], prefix5.DiagnosticLevel.debug, #minLevel), r.ParameterMirrorImpl(r'key', 67252230, 27, const prefix0.Reflector(), -1, -1, -1, null, const [], null, #key), r.ParameterMirrorImpl(r'serviceA', 134358022, 27, const prefix0.Reflector(), 5, -1, -1, null, const [], null, #serviceA), r.ParameterMirrorImpl(r'serviceB', 134358022, 27, const prefix0.Reflector(), 6, -1, -1, null, const [], null, #serviceB), r.ParameterMirrorImpl(r'other', 134348806, 31, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null), r.ParameterMirrorImpl(r'i', 134348806, 39, const prefix0.Reflector(), -1, -1, -1, null, const [], null, null)], <Type>[prefix1.Widget1, prefix1.Widget2, prefix2.ApiServiceIF, prefix2.ApiService, prefix2.ApiServiceProd, prefix3.ServiceA, prefix3.ServiceB, prefix3.ServiceC], 8, {r'==': (dynamic instance) => (x) => instance == x, r'toString': (dynamic instance) => instance.toString, r'noSuchMethod': (dynamic instance) => instance.noSuchMethod, r'hashCode': (dynamic instance) => instance.hashCode, r'runtimeType': (dynamic instance) => instance.runtimeType, r'toStringShort': (dynamic instance) => instance.toStringShort, r'toDiagnosticsNode': (dynamic instance) => instance.toDiagnosticsNode, r'debugFillProperties': (dynamic instance) => instance.debugFillProperties, r'toStringShallow': (dynamic instance) => instance.toStringShallow, r'toStringDeep': (dynamic instance) => instance.toStringDeep, r'debugDescribeChildren': (dynamic instance) => instance.debugDescribeChildren, r'key': (dynamic instance) => instance.key, r'createElement': (dynamic instance) => instance.createElement, r'build': (dynamic instance) => instance.build, r'serviceA': (dynamic instance) => instance.serviceA, r'serviceB': (dynamic instance) => instance.serviceB, r'serviceC': (dynamic instance) => instance.serviceC, r'createState': (dynamic instance) => instance.createState, r'handle': (dynamic instance) => instance.handle, r'getText': (dynamic instance) => instance.getText, r'getCount': (dynamic instance) => instance.getCount}, {}, <m.LibraryMirror>[r.LibraryMirrorImpl(r'', Uri.parse(r'reflectable://0/'), const prefix0.Reflector(), const <int>[], {}, {}, const [], null), r.LibraryMirrorImpl(r'', Uri.parse(r'reflectable://1/'), const prefix0.Reflector(), const <int>[], {}, {}, const [], null), r.LibraryMirrorImpl(r'', Uri.parse(r'reflectable://2/'), const prefix0.Reflector(), const <int>[], {}, {}, const [], null)], [])};
   │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ^^^^^^^^^^
   ╵
[INFO] Running build completed, took 9.3s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 13ms

[SEVERE] Failed after 9.3s
pub finished with exit code 1

Presumably your package has an sdk constraint whose value is less than 2.15 (which is the version of Dart where support for explicit generic instantiation was introduced). When that feature is not supported, inject<ServiceA> is a syntax error.

@eernstg - interesting that's good to know. But I am using sdk: '>=2.18.4 <3.0.0' and reflectable: ^4.0.2. And there are no compilation errors in the IDE.

https://github.com/dinbtechit/flutter_reflection_test/blob/master/pubspec.yaml#L9

environment:
  sdk: '>=2.18.4 <3.0.0'

Check out the code in *.reflectable.dart: Does it contain a comment of the form // @dart = 2.9? Any version which is smaller than 2.15 would cause the error as reported, but the reflectable code generator only uses this version, and only in the case where some parts of the program are not yet migrated to null safety.

In that case (where some parts of your program are not yet null safe), there's no way to follow the migration guidelines and give the generated code a higher language version, because migrated code is not supposed to import legacy code (and the generated code generally needs to import everything which is reachable by reflection).

If you have to use language version 2.9 with the generated code then you can still express the generic function instantiation "manually":

// @dart = 2.9

X id<X>(X x) => x; // A generic function.

void main() {
  // We'd like to do `var intId = id<int>;`, but that requires Dart 2.15.
  int Function(int) intId1 = id; // Perform the generic instantiation implicitly.
  var intId2 = (int i) => id<int>(i); // Use a wrapper function.
}

Ahh I see!!! It does say @dart= 2.12 in the generated code...

// This file has been generated by the reflectable package.
// https://github.com/dart-lang/reflectable.
// @dart = 2.12

However, my code is null-safe. I also ran the dart migrate command to confirm that. I am not sure why it would use dart 2.12.

╰$ dart migrate                                                
Migrating /Users/user/code/personal/flutter_reflection_test

See https://dart.dev/go/null-safety-migration for a migration guide.

Analyzing project...
[--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------]All sources appear to be already migrated.  Nothing to do.

Oops, you're right, that's a bug (it was needed when null safety was experimental, it is not needed and indeed harmful today).

Thank you for the quick fix! I was able to successfully generate the code. However, it does not automatically import the Classes and functions in the generated code.

Generated Code:
image

Sounds like another bug. Do you have a small example that shows the behavior?

Here are the steps to reproduce:

  1. Create a dart file, import_issue.dart with following contents

    import_issue.dart

    @GlobalQuantifyMetaCapability(Component, Reflector())
    
    import 'package:reflectable/reflectable.dart';
    import 'generics_import_issue.reflectable.dart';
    
    class Reflector extends Reflectable {
    const Reflector()
      : super(
      newInstanceCapability,
      declarationsCapability,
      invokingCapability,
      typeCapability,
      typeRelationsCapability,
      libraryCapability,
      metadataCapability);
    }
    
    class Component {
    final Object value;
    const Component({required this.value});
    }
    
    //  -----------------------------------------------
    
    abstract class Service {}
    
    T someFunction<T>() {
    return Object() as T;
    }
    
    
    @Component(value: someFunction<Service>)
    class A {
    
    }
    
    void main() {
    initializeReflectable();
    // Now go to the genereted code import_issue.reflectable.dart to see the compile error.
    }
  2. Run build_runner

    flutter pub run build_runner watch
    # or, for straight dart
    dart pub run build_runner watch 
  3. Open the generated code: import_issue.reflectable.dart. Approximately at line 40. You will see a compile error.
    despite the library code being already imported. But since it is using the aliasas prefix0 it's not able to resolve the function and the class.

    import 'import_issue.dart' as prefix0; 

    Note - The component is correctly generated as prefix0.Component

    image

  4. The generated code should use the alias - prefix0 to refer to the somFunction and Service class to resolve the compile errors.

    image

Hope this helps. Let me know if you need more info.

That's great, thanks!

#303 should do it.

Workaround, until 4.0.4 is uploaded:

// Replace:
@Component(value: someFunction<Service>)

// By:
const someFunctionService = someFunction<Service>;
@Component(value: someFunctionService)