Closed dinbtechit closed 1 year ago
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).
Cf. reflectable 4.0.3.
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:
Sounds like another bug. Do you have a small example that shows the behavior?
Here are the steps to reproduce:
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.
}
Run build_runner
flutter pub run build_runner watch
# or, for straight dart
dart pub run build_runner watch
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 asprefix0.Component
The generated code should use the alias - prefix0
to refer to the somFunction
and Service
class to resolve the compile errors.
Hope this helps. Let me know if you need more info.
That's great, thanks!
https://github.com/google/reflectable.dart/pull/303 should do it.
Workaround, until 4.0.4 is uploaded:
// Replace:
@Component(value: someFunction<Service>)
// By:
const someFunctionService = someFunction<Service>;
@Component(value: someFunctionService)
Hi, Unable to parse the constructor with "Function with generics". Is it a bug? Or am I doing something wrong?
My Reflectable implementation
Full Error: