How can I cancel a 'await for' in a flutter / dart bloc - flutter

I am using a stream to read out location data in a bloc. I have a start and a stop event. In the stop method, I cancel the stream subscription. When I use listen to a stream to yield the state the inside where the yield statement is never gets called.
Stream<LocationState> _start() async* {
_locationSubscription = location.onLocationChanged.listen(
(location) async* {
if (location.isNotNull) {
yield LocationState.sendData(location: updateLocation(location));
}
},
);
//send one initial update to change state
yield LocationState.sendData(
location: updateLocation(await Location().getLocation()));
}
Stream<LocationState> _stop() async {
await _locationSubscription?.cancel();
_locationSubscription = null;
yield LocationState.stoped();
}
When I replace the listen to await for I don't see any way
to stop this from yielding events because the subscription handle is gone.
Any ideas? Any explanations?
Stream<LocationState> _start() async* {
await for (LocationData location in location.onLocationChanged) {
if (location.isNotNull) {
yield LocationState.sendData(location: updateLocation(location));
}
}
//send one initial update to change state
yield LocationState.sendData(
location: updateLocation(await Location().getLocation()));
}

The problem is that I did not understand the behavior of yield completely.
Also, the dart framework has some shortcomings.
The problem was discussed in detail with the dart makers, here.
https://github.com/dart-lang/sdk/issues/42717
and here
https://github.com/felangel/bloc/issues/1472

Related

Migrate to BLoC 7.2- Nested Streams - yield* inside other stream

I'm migrating a project from Bloc 7.0 to 7.2
I have an issue trying handle the migration of this following Stream since it is calling another Stream within it self :
Stream<CustomerState> _mapUpdateNewsletter({...}) async* {
try {
[...]
yield* _mapGetCustomer(); // Calling another Stream here
Toast.showSuccess(message: successMessage);
} ...
}
Here is what the called Stream used to look like
Stream<CustomerState> _mapGetCustomer() async* {
try {
final customer = await _customerRepository.getCustomer();
yield state.getCustomerSuccess(customer);
} catch (error, stackTrace) {
ApiError.handleApiError(error, stackTrace);
}
}
Here is what I migrated it to :
Future<void> _onGetCustomer(
GetCustomer event, Emitter<CustomerState> emit) async {
try {
final customer = await _customerRepository.getCustomer();
emit(state.getCustomerSuccess(customer));
} catch (error, stackTrace) {
ApiError.handleApiError(error, stackTrace);
}
}
How am I suppose to call it now in Bloc 7.2 ?
Future<void> _onUpdateNewsletter(UpdateNewsletter event, Emitter<CustomerState> emit) async {
try {
...
yield* _onGetCustomer; // How do I call this async future here?
Toast.showSuccess(message: event.successMessage);
} ...
}
in the new version of the bloc, you don't have to write stream functions. you have a function called emit and calling this function and passing the new state is possible from every function in your bloc. so remove yield* and just call _onGetCustomer function and from there emit your new state.

Can Dart Streams emit a value if the stream is not done within a duration?

I am working on a Flutter app using blocs to control the state of the view. I want to call and external API and if it responds quickly, show the results right away by yielding the result state. However, if the call takes more than, say, 5 seconds, I would like to yield a state indicating that the response is taking a while while still waiting for the API to return. How can I do this with Dart Streams, either natively or with RxDart?
This can be accomplished using Stream.timeout. Thanks #pskink!
Stream<String> delayedCall() async* {
yield 'Waiting';
final apiCall = Future.delayed(Duration(seconds: 5)).then((_) => 'Complete');
yield* Stream.fromFuture(apiCall).timeout(
Duration(seconds: 3),
onTimeout: (eventSink) => eventSink.add('Still waiting'),
);
}
void main() {
final stream = delayedCall();
stream.listen(print);
}
All the Futureshave a delayed timeout that you can use for example
try{
var timer = Timer(Duration(seconds: 3), () {
yield SlowAPI();
});
YourFeature.whenComplete((){
timer.cancel();
});
yield Success();
}catch(e){
yield Error();
}

Can't yield in forEachAsync inside Stream in dart/flutter

I have a forEachAsync inside an async* Stream and can't yield.
Stream<ProjectState> _mapProjectSelectedEventToState(ProjectSelected event) async* {
try {
yield ProjectLoading(
message: 'Fetching database',
fetchedCount: 0,
totalCount: 1,
);
await forEachAsync(fileModels, (FileEntity fileModel) async {
await downloader.download(filename: fileModel.hashName);
_totalMediaFilesFetched++;
//// ERROR - THIS DOES NOT WORK ////
yield (ProjectLoadingTick(
_totalMediaFiles,
_totalMediaFilesFetched,
));
}, maxTasks: 5);
} catch (error, stacktrace) {
yield ProjectFailure(error: error);
}
}
I've tried other means by dispatching the message and converting it to a state but it doesn't work as well. It seems like the whole app is blocked by this await forEachAsync.
I'm using the bloc pattern which reacts to the emited ProjectStates based on the current ProjectSelected event
Your attempt doesn't work because you're using yield in a callback, not in the function that's returning a Stream. That is, you're attempting the equivalent of:
Stream<ProjectState> _mapProjectSelectedEventToState(ProjectSelected event) async* {
...
await forEachAsync(fileModels, helperFunction);
...
}
Future helperFunction(FileEntity fileModel) async {
...
yield ProjectLoadingTick(...);
}
which doesn't make sense.
Since care about forEachAsync's ability to set a maximum limit to the number of outstanding asynchronous operations,
you might be better off using a StreamController that you can manually add events to:
var controller = StreamController<ProjectState>();
// Note that this is not `await`ed.
forEachAsync(fileModels, (FileEntity fileModel) async {
await downloader.download(filename: fileModel.hashName);
_totalMediaFilesFetched++;
controller.add(ProjectLoadingTick(
_totalMediaFiles,
_totalMediaFilesFetched,
));
},
maxTasks: 5);
yield* controller.stream;

Async request using BLoC in Flutter

I would like download the data, but also use the application all the time.
Can you tell me if it's right solution?
The case is we press button download and call funtion bloc.dispatch(Event.download());
In mapEventToState in _Download event we reqest data. But we don't wait for response because we don't want to block others events which are changing view.
So I create Future and after getting response I call event _UpdateData() where I process downloaded data and generate state with them.
It's ok?
There is _requestTime parameter to check if it's last request.
class Bloc {
DateTime _requestTime;
#override
Stream<State> mapEventToState(Event event) async* {
if (event is _Download) {
yield DownloadingState();
_request();
} else if (event is _UpdateData) {
if(!event.requestTime.isBefore(_requestTime))
yield DownladedState(event.response);
}
}
_request() {
_requestTime = DateTime.now();
repository.downloadData().then((response) {
dispatch(_UpdateData(response));
});
}
}
Let me know if it works
Changeadded yield* in front of _request
#override
Stream<State> mapEventToState(Event event) async* {
if (event is _Download) {
yield DownloadingState();
yield* _request();
} else if (event is _UpdateData) {
if(!event.requestTime.isBefore(_requestTime))
yield DownladedState(event.response);
}
}
_request() async*{
_requestTime = DateTime.now();
repository.downloadData().then((response) {
dispatch(_UpdateData(response));
});
}
}

What is the proper way to finalize a method that is async*

I have this method that tries to login into a service. If the login has successful it query the bonus and clientName. Otherwise it shoulds send a message and have quit the method.
So if it verifies the session is not logged, I send a message and uses return to quit method. I wonder if this is the correct way to do that, it seems a little weird. Is there a better way to finalize this kind of method?
#override
Stream<GetUserInfoState> mapEventToState(
GetUserInfoEvent event,
) async* {
if (event is GetUserInfoEventImpl) {
yield ConnectingState();
var session = event.session;
await for (final message in session.login()){
yield new MessageState(message);
}
if (!await session.isLogged) {
yield new MessageState('Could not connect');
return;
}
var bonus = await session.bonusPoints;
var clientName = await session.clientName;
yield new SuccessfulState(clientName, bonus, getMessage());
}
}