StreamSubscription and StreamBuilder - flutter

I want to cancel a stream, and found that a Stream cannot be canceled, but a StreamSubsciption can be canceled. Stream object has a listen() method which
Adds a subscription to this stream.
What actually is a subscription? and how do I use it with StreamBuilder?
From the docs:
A subscription on events from a Stream.
When you listen on a Stream using Stream.listen, a StreamSubscription object is returned.
The subscription provides events to the listener, and holds the callbacks used to handle the events. The subscription can also be used to unsubscribe from the events, or to temporarily pause the events from the stream.
From what I understand, I think I can use the event and use it to rebuild my widgets. Is it the right way to do it? or is there is a more correct way to do it?

If you are using StreamBuilder, stream subscription and stream cancelling is managed by the framework. The builder method will be called when new snapshot is available, and when the widget is destroyed, stream subscription is cancelled.
As you wrote, you can create your own StreamSubscription with listen() method, and in this case you have to cancel this subscription when you no longer need it, for example in an overridden dispose method. To do so keep track of subscription in a variable or a member, assuming you have a stream in stream variable and MyType is the class which is returned by stream:
StreamSubscription<MyType> myStreamSubscription = stream.listen((value) {
// add code here
});
Then you can use this to manage the stream:
myStreamSubscription.pause();
myStreamSubscription.resume();
myStreamSubscription.cancel();

Related

Flutter Blocs quick events leads to data loss

I am new to flutter and the famous flutter_bloc library. I have created a chat system based on the BLoC pattern :
A widget fires an event to chatBloc
The chatBloc catches the event, process it and call chatRepository
ChatRepository adds the message as a draft in its messageList variable and then sends the message to the API
ChatRepository receives the message from the API when it is done and replaces the draft within its messageList variable
ChatBloc yields its final state and the page rebuilds
Everything works fine as long as no more than one message is processed at a time.
If my button widget fires two quick events, they will overlap and so the second event will fire while the first event is still in progress. By the time when the second event starts, the first message is still a draft, so when message 2 completes and chatRepository updates its messageList, it will treat message 1 as a draft even if it has been validated.
At this point I'm asking myself if I made a mistake or if I'm trying to do it the wrong way.
Would it be better to create a queue and process messages in the background one by one ?
Thank you for your help !

How can we get the events of calling on Agora Video Call on Flutter?

How can I get the following events?
Initiate Call event
Call received event
Call missed event
Call end event
You can use onConnectionStateChanged() callback for most of these functions.
You can read about these functions over here:
https://docs.agora.io/en/Video/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_channel_event_handler.html#a6dfbfe241ac004b792a499c29033ddff

how can i access to data from another bloc

hey guys i'm new with bloc pattern and i have some questions
How I can get props data from a State inside BlocBuilder for another bloc ?
already worked with provider (state management) and it's easy to access to any data from everywhere , is that possible with bloc and how ?
thank you
Hoping that I understood your question correctly,
You have a piece of data stored in state in bloc A and you want to use that data in bloc B.
For instance a post Bloc which needs to fetch all the posts by a logged in user (assuming that the logged in user has been stored in state in the user Bloc).
Defining the event:
class FetchPost extends PostEvent{
final User currentUser
}
when dispatching the FetchPost event:
Note: if you are dispatching the event from your view, you have to get the bloc from Provider.of(context)
PostBloc _postBloc = Provider.of<TransactionBloc>(context);
UserBloc _userBloc = Provider.of<UserBloc>(context);
_postBloc.dispatch(FetchPost(_userBloc.currentState.loggedInUser))
To access other blocs props you have to hold its instance, and from it, you can acquire its state. I have addressed this in the blog post that I have written.
thanks guy i think i find the solution of this question
ist's data shouldn't be in the bloc, data should be in a repository. Then, you can use RepositoryProvider or MultiRepositoryProvider in order to get any data from any bloc in the subtree.

Recognize removal of Widget in Cockpit Dashboard on Cumulocity

Is there a way (maybe an event or something we can subscribe to) to acknowledge inside a self implemented widget, whenever this or another widget is removed from the dashboard?
There is nothing like an event you can subscribe to.
In the end there will always be a PUT request to update the dashboard object whenever it changes. Maybe you can utilize that.

How can we call event handler asynchronously for an event

Is there a way to subscribe to an event and handle it asynchronously using the event+=EventHandler
syntax or is there any workaround to achieve it
The event/delegate system calls each of the subscribed event handles synchronously on the thread the fires the event. To make the event handler processing be done asynchrounously, it has to be part of the function that you subscribe to the event.