I have this Future and my code template like this:
Future getDevices() async {
stream.listen();
Timer.periodic(Duration(seconds:5), (timer) {
POST TO SERVER.then((value){
return Future.value(value);
});
});
}
I'm Listening to a Stream to scan for beacon devices. I fill the yield named "beacons" in the Listen function of this Stream. With Timer.periodic, I control the yield named "beacons" and perform POST operation. I want to return Future.value on "then" of this POST operation. But Future returns null without waiting for the result of POST operation.
I tried Completer like this:
Future getDevices() async {
final completer = Completer();
stream.listen();
Timer.periodic(Duration(seconds:5), (timer) {
POST TO SERVER.then((value){
return completer.complete(value);
});
});
return completer.future;
}
but this time I also got this error: "Unhandled Exception: Bad state: Future already completed"
EDIT: I try to use Stream.asyncMap but same result when i did it.
Stream myStream = Stream.periodic(Duration(seconds: 5), (timer) async {
beacons.removeWhere((key, value) =>
DateTime.now().difference(value['lastUpdate']).inSeconds > 6);
if (beacons.isNotEmpty) {
bool processResult = false;
beacons.forEach((key, value) async {
if (int.parse(value['onlyEnterance'].toString()) == 1 &&
double.parse(value['distance'].toString()) <
double.parse(value['minDistance'].toString())) {
await userRepo
.createPayrollTracking(context, forEnter, value['dbId'])
.then((value) {
processResult = value;
if (value == true) {
stopMonitoring();
}
});
return await Future.value(processResult);
}
});
}
}).asyncMap((event) async => await event);