riverpod
riverpod copied to clipboard
Provider with watch can not run listen's lambda when app is in background.
Describe the bug I have a stateProvider. And I have a Provider that watches stateProvider. When app is foreground, provider and stateProvider can listen and run successfully. But when I turn the app to background, only stateProvider can run listen's lambda, Provider can't run listen's lambda.
To Reproduce
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() => runApp(const ProviderScope(
child: RiverpodProviderApp(),
));
class RiverpodProviderApp extends ConsumerStatefulWidget {
const RiverpodProviderApp({Key? key}) : super(key: key);
@override
ConsumerState createState() => _RiverpodProviderAppState();
}
final stateProvider = StateProvider<int>((ref) => 0);
final provider = Provider<int>((ref) => ref.watch(stateProvider));
class _RiverpodProviderAppState extends ConsumerState<RiverpodProviderApp> {
@override
void initState() {
super.initState();
Timer.periodic(const Duration(seconds: 2), (timer) {
final stateNotifier = ref.read(stateProvider.notifier);
final value = stateNotifier.state;
stateNotifier.state = value + 1;
});
}
@override
Widget build(BuildContext context) {
ref.listen(provider, (previous, next) {
print('provider previous:$previous, next:$next');
});
ref.listen(stateProvider, (previous, next) {
print('stateProvider previous:$previous, next:$next');
});
return Container();
}
}
Expected behavior When the exmple app run in foreground, both listen successfully.
I/flutter (12055): stateProvider previous:62, next:63
I/flutter (12055): provider previous:62, next:63
I/flutter (12055): stateProvider previous:63, next:64
I/flutter (12055): provider previous:63, next:64
I/flutter (12055): stateProvider previous:64, next:65
I/flutter (12055): provider previous:64, next:65
But when the exmple app run in background, only stateProvider listen successfully.
I/flutter (12055): stateProvider previous:305, next:306
I/flutter (12055): stateProvider previous:306, next:307
I/flutter (12055): stateProvider previous:307, next:308
I/flutter (12055): stateProvider previous:308, next:309
I/flutter (12055): stateProvider previous:309, next:310
I/flutter (12055): stateProvider previous:310, next:311
I/flutter (12055): stateProvider previous:311, next:312
I/flutter (12055): stateProvider previous:312, next:313
I guess the reason is that provider's update depend on drawFrame, but when app is in background, drawFrame not call. Can you give some suggestion or solution?
@rrousselGit Can give some suggestion or solution?
Providers are in sync with the app. If the app is paused, providers are paused too.
You'd have to manually read the provider. Then it would forcibly be updated
@rrousselGit I want to handle server push when app is in background. I will update the stateProvider, but Provider who watch it can't listen the changes. What is the best way to solve it?
As I said, reading the provider will cause it to rebuild. You could do that when your app is in background
It's not really a bug. So I'll close it.
But if desired, we could have a flag for disabling this behavior. Feel free to open a new issue if you want it. I don't plan on working on it though