You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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';
});
classHomeextendsStatelessWidget {
@overrideWidgetbuild(BuildContext context) {
returnScaffold(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
The text was updated successfully, but these errors were encountered:
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';
});
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"
I am opening this issue based on conversation on #193
try to switch
with
what I found is, if I watch futureProvider first, then I'l' got infinite log like this
But if I switch the order, it will be okay
here's the log if I watch StreamProvider first
The text was updated successfully, but these errors were encountered: