I am using RiverPod in the following basic app which listens to intStreamProvider. How do I know for sure that the stream is disposed when I terminate the app. Currently, when the app starts, this message is printed to the console "===> created stream provider". However, when I terminate the app, this message is NOT printed to the console '===> disposed stream provider'. Why is that? Please follow the comments in the code.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// THIS IS THE STREAM THAT I WOULD LIKE DISPOSED WHEN TERMINATING THE APP
// How do I know for sure that this stream was disposed when app terminates?
final intStreamProvider = StreamProvider.autoDispose<int>((ref) {
// MESSAGE PRINTED ON CREATION
debugPrint('===> created stream provider');
// MESSAGE DOES NOT PRINT ON TERMINATION OF APP
ref.onDispose(() => debugPrint('===> disposed stream provider'));
return Stream.value(0);
});
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: MyHomePage());
}
}
class MyHomePage extends ConsumerWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
// WATCHING THE STREAM OVER HERE
ref.watch(intStreamProvider);
return Scaffold(body: Container());
}
}
When apps are stopped, the process is killed.
This doesn't "dispose" providers or widgets; it simply stops the program.
Streams will be killed too though, as the program is killed, so they are killed along with it.
But you cannot perform an action before the program is about to get killed. At least not using dispose
. There are platform-specific solutions. But that's a different question