Currently, both Riverpod and flutter_hooks sometimes relies on users having to define a list of dependencies used by a function
An example would be:
final provider = Provider<MyClass>((ref) {
ref.watch(dependency);
ref.watch(anotherDependency);
return MyClass();
},
dependencies: { dependency, anotherDependency }, // the list of providers passed to "ref.watch"
name: 'provider',
);
or:
Widget build(context) {
final cachedResult = useMemoized(
() => MyClass(dependency),
[dependency], // the list of variables used within the closure
);
final another = useMemoized(() => MyClass(foo, bar: bar), [foo, bar]);
...
}
The problem with this syntax is, it is error prone.
The interesting thing is, in both examples, this list of dependency is known at compile time.
This means that this secondary parameter could instead be generated by a macro directly with a 100% accuracy.
In particular, I was thinking of changing the first example to be:
@provider
MyClass $provider(ProviderReference ref) {
ref.watch(dependency);
ref.watch(anotherDependency);
return MyClass();
}
which would generate:
final provider = Provider($provider, dependencies: { dependency, anotherDependency }, name: 'provider');
Sadly it seems like the current macro API does not give us enough informations about the function content to do such a thing.
Currently, both Riverpod and flutter_hooks sometimes relies on users having to define a list of dependencies used by a function
An example would be:
or:
The problem with this syntax is, it is error prone.
The interesting thing is, in both examples, this list of dependency is known at compile time.
This means that this secondary parameter could instead be generated by a macro directly with a 100% accuracy.
In particular, I was thinking of changing the first example to be:
which would generate:
Sadly it seems like the current macro API does not give us enough informations about the function content to do such a thing.