Skip to content

Use-case: statically extract the dependencies of a function #37

Description

@rrousselGit

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions