Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamProvider.autoDispose : disposed and re run when reference in futureProvider #243

Closed
muhajirdev opened this issue Dec 12, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@muhajirdev
Copy link

I am opening this issue based on conversation on #193

final testProvider = StreamProvider.autoDispose((ref) async* {
  ref.onDispose(() {
    print('testProvider disposed');
  });

  yield '1';
  yield '2';
  yield '3';
  yield '4';
});

final someFutureProvider = FutureProvider((ref) async {
  print('future is called');
  return 'something';
});

final test2Provider = FutureProvider.autoDispose((ref) async {
  final somefuture = await ref.watch(someFutureProvider.future);
  print('somefuture is $somefuture');
  final a = await ref.watch(testProvider.last);
  print('a is $a');

  return 'c';
});

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(
      child: Consumer(
        builder: (context, watch, child) {
          final u = watch(test2Provider);
          return u.when(
              data: (a) => Text(
                    "anything",
                    style: TextStyle(color: Colors.white),
                  ),
              loading: () => Text(
                    'a',
                    style: TextStyle(color: Colors.white),
                  ),
              error: (e, s) => Text(
                    'b',
                    style: TextStyle(color: Colors.white),
                  ));
        },
      ),
    ));


  }}

try to switch

 final somefuture = await ref.watch(someFutureProvider.future);
  print('somefuture is $somefuture');
  final a = await ref.watch(testProvider.last);
  print('a is $a');

with

final a = await ref.watch(testProvider.last);
print('a is $a');
final somefuture = await ref.watch(someFutureProvider.future);
print('somefuture is $somefuture');

what I found is, if I watch futureProvider first, then I'l' got infinite log like this

flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed
flutter: somefuture is something
flutter: a is 1
flutter: testProvider disposed

But if I switch the order, it will be okay

here's the log if I watch StreamProvider first

flutter: a is 1
flutter: future is called
flutter: somefuture is something
flutter: a is 4
flutter: somefuture is something
@muhajirdev muhajirdev added bug Something isn't working needs triage labels Dec 12, 2020
@rrousselGit
Copy link
Owner

Hum I don't think that's a bug, but it's an interesting issue

Basically what is happening is, the state of testProvider was destroyed before while test2Provider is awaiting for someFutureProvider, testProvider has no remaining listener.
This happens because the dependency on testProvider by test2Provider is removed right before the state is recreated.

One fix would be:

final test2Provider = FutureProvider.autoDispose((ref) async {
  final aLast = ref.watch(testProvider.last);

  final somefuture = await ref.watch(someFutureProvider.future);
  print('somefuture is $somefuture');
  final a = await aLast;
  print('a is $a');

  return 'c';
});

@rrousselGit
Copy link
Owner

The main reason why this is happening is because you could technically write:

if (ref.watch(foo)) {
  ref.watch(bar);
}

which means in some cases, the provider should rebuild when bar update, and in other cases, it shouldn't. Hence why the list of dependencies is cleared before the state is recreated, and hence why the state of autoDispose providers is destroyed when using "await"

@rrousselGit
Copy link
Owner

Closing as this isn't a bug, but more of an edge case

I'd suggest listening to the provider that you don't want to be disposed in a widget, even if the widget doesn't use its value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants