Reputation: 1
I need to return result from function if stream returns no connection value.
So i have outer function and inner function. How can i return result from outer inside inner?
Future<bool> send() async {
var subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
if(result == ConnectivityResult.none)
throw("No connectivity");
// Need to stop `send` function here and return some result
});
await doSomeWork();
return true;
}
Upvotes: 0
Views: 1337
Reputation: 10935
The Future
returned by send
can only be completed once, while the Stream
may have multiple events emitted - you can't complete it with true
and then later complete again with false
because some new event came through the stream.
You could decide that the return value from send
depends on the first event in the stream:
Future<bool> send() async {
var result = await Connectivity().onConnectivityChange.first;
if (result == ConnectivityResult.none) {
// handle no connection
return false;
}
await doSomeWork(); // only happens if the first result was not `none`
return true;
}
Or you'll need to make a decision on what it means for a none
connectivity result to come through after doSomeWork()
was already called and the Future
returned from send
already completed.
Upvotes: 3