Comments (3)
I'm having a similar problem
from riverpod.
Theres nothing wrong here. Your provider wasn't disposed. So the Stream is still active.
Use autoDispose.
from riverpod.
Perhaps the test setup is just confusing. I will instead provide code closer to the real use case below.
Our app has a provider which exposes a stream from an event channel, basically just return eventChannel.receiveBroadcastStream()
. On the Java side, we start using HW resources when the event channel is listened to (in the Java onListen()
method) and stop using them when the event channel no longer has any listeners (in the Java onCancel()
method). The problem is that the event channel stream never gets unsubscribed from and therefore the Java onCancel()
method never gets executed.
In the code below I have replaced the EventChannel object with a simple StreamController().stream
object to keep things simple and in a single file. But think of it as an event channel.
When toggling between the two views I expect to see both Event: onListen
and Event: onCancel
being printed. But only Event: onListen
gets printed once.
The goal is to make the onCancel()
method on the Java side being called when no widget is listening to the event channel stream. How can that be achieved?
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// This is simplified code (to not have to implement Java code).
final eventChannelStreamController = StreamController<int>();
final eventChannelStream = eventChannelStreamController.stream;
// More similar to real code:
// const eventChannel = EventChannel('event_channel');
// final eventChannelStream = eventChannel.receiveBroadcastStream();
final streamProvider = StreamProvider.autoDispose<int>((ref) {
ref.onDispose(() {
// Should something be done here?
});
return eventChannelStream.asBroadcastStream();
});
void main() {
eventChannelStreamController.onListen = () {
print('Event: onListen');
};
eventChannelStreamController.onCancel = () {
print('Event: onCancel');
};
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
bool showTestWidget = true;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body:
showTestWidget ? const TestWidget() : const Text('Another widget'),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => showTestWidget = !showTestWidget),
child: Icon(showTestWidget ? Icons.visibility_off : Icons.visibility),
),
),
);
}
}
class TestWidget extends ConsumerWidget {
const TestWidget({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return ref.watch(streamProvider).when(
data: (value) => const Text('TestWidget got data'),
loading: () => const Text('TestWidget loading'),
error: (error, stack) => Container());
}
}
from riverpod.
Related Issues (20)
- Read a "disposed" notifier HOT 1
- Refactoring Propositions dropping out when updating flutter HOT 2
- Add exception handling to 'ref.refresh()' HOT 6
- Add something like `ref.watchStream(...)` HOT 10
- ProviderScope state wrongly disposed possible bug HOT 5
- Add Japanese docs
- [riverpod_generator] `dependencies` does not respect `provider_family_name_suffix` option
- StateNotifierProvider always refreshs when invalidated problem still exists HOT 3
- Error: 'LintCode' is imported from both 'package /src/dart/error/lint_codes.dart' and 'package /src/lint_codes.dart' on Flutter 3.24.0 with build_runner HOT 3
- VS Code refactoring options are not available with Flutter 3.24 stable release HOT 4
- `initialValue`-like behaviour for the FutureProvider HOT 7
- `flutter` dependency included when using Riverpod in a server-side environment HOT 1
- ref.watch doesn't work HOT 2
- invalidate provider on widget dispose() HOT 1
- Why might Riverpod destroy a provider? HOT 2
- AsyncLoading progress value doesn't propagate when setting state from AsyncLoading to AsyncLoading
- Updating AsyncNotifier state if it contains error HOT 2
- Publish new versions to pub.dev HOT 1
- [riverpod_lint] riverpod_lint sometime didn't show assists (in context menu)
- Provider throws "Bad state" error when ref.invalidate is called after ref.read HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from riverpod.