my code as below(report error)
@override
Stream<String> mapEventToState(Map event)async* {
Future<String> f = request1(event);
yield* f.asStream();
}
Future<String> request1(Map m) async {
//rxdart+dio request data
var header = {
"Authorization": "Basic d2ViQXBwOndlYkFwcA==",
};
Observable<Response<String>>response= await Observable(Dio()
.post<String>("http://******:30099//zuul/api-auth/oauth/token",
queryParameters: m, options: Options(headers: header))
.asStream());
response.listen((rs) {
if (rs.statusCode == 200) {
print("success");
return Future.value("1111");
} else {
print("error");
return Future.value("22222");
}
});
i don't kown when i set listen,the code report exception
and other way (right)
Response<String> rs = await Dio().post<String>(
"http://*****:30099//zuul/api-auth/oauth/token",
queryParameters: m,
options: Options(headers: header));
print("done");
if (rs.statusCode == 200) {
print("success");
return Future.value(rs.data);
} else {
print("error");
return Future.value("error");
}
Hi @wxy520ll 馃憢
Thanks for opening an issue!
Just to clarify, the second way works fine and the first way throws an exception?
In general, I would not recommend subscribing to streams within mapEventToState because you block the event loop. Instead, I would recommend subscribing to whatever stream you have in the bloc's constructor and dispatching events in response to new data. Check out this example.
Hope that helps! 馃憤
thank you very much
Most helpful comment
thank you very much