Bloc: Unhandled Exception: 'package:bloc/src/transition.dart': Failed assertion: nextState != null': is not true

Created on 1 May 2019  路  2Comments  路  Source: felangel/bloc

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");
      }
question

Most helpful comment

thank you very much

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Reidond picture Reidond  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments

ricktotec picture ricktotec  路  3Comments

abinvp picture abinvp  路  3Comments