riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

Detect possible optimization when chaining `await ref.watch`

Open rrousselGit opened this issue 11 months ago • 3 comments

One way applications can become slow is by executing code sequentially when it could be parallelized.

The l'inter could detect possible optimization when using Ref.watch and suggest a more efficient code.

Bad:

final  a=await ref.watch(a.future)
final b =await ref.watch(a.future)

rrousselGit avatar Jan 18 '25 16:01 rrousselGit

May I ask a better way? Future.wait loses type safety.

lishaduck avatar Apr 01 '25 14:04 lishaduck

@lishaduck This works in dart:

final (a, b) = await (ref.watch(a.future), ref.watch(b.future)).wait;

Disclaimer: I am not deeply familiar with Riverpod internals, so there may be a more idiomatic way to achieve this within Riverpod.

rubenferreira97 avatar Apr 01 '25 15:04 rubenferreira97

@lishaduck This works in dart

That's a thing!? I never knew that. Thanks!

lishaduck avatar Apr 01 '25 15:04 lishaduck