Home > Net >  Flutter bloc migration
Flutter bloc migration

Time:10-05

I am having a problem migrating to a new version of flutter_bloc. This:

Stream<NewsState> mapEventToState(NewsEvent event) async* {
    if (event is FetchTopAndOtherNewsEvent) yield* _repository.fetchTopAndOtherNews(event);
    if (event is FetchNewsEvent) yield* _repository.fetchNews(event);
  }

I rewrite as:

class NewsBloc extends Bloc<NewsEvent, NewsState> {
  final NewsRepository _repository = NewsRepository();
  NewsBloc() : super(NewsInitialState()){
    on((event, emit) {
      if (event is FetchTopAndOtherNewsEvent) emit(_repository.fetchTopAndOtherNews(event));
      if (event is FetchNewsEvent) emit(_repository.fetchNews(event));
    });
  }

But understandably it gives an error with the argument type 'Stream' cannot be assigned to the parameter type 'NewsState'.

CodePudding user response:

I currently I use like

class NewsBloc extends Bloc<NewsEvent, NewsState> {
  NewsBloc() : super(NewsInitial()) {
    on<FetchTopAndOtherNewsEvent>(_fetchTopAndOtherNewsEvent);
    on<FetchNewsEvent>(_onFetchNewsEvent);
  }

  _fetchTopAndOtherNewsEvent(FetchTopAndOtherNewsEvent event, emit) { /// event job
}

  _onFetchNewsEvent(FetchNewsEvent event, emit) {/// event job
 }
}

More about flutterbloccoreconcepts

  • Related