In Flutter, how do I replace 'transformEvents' with `on<Event>` with an `EventTransformer`?

Viewed 288

I am following along with this tutorial and up to this point have been understanding and bringing the code up to the most recent standard (null safety, changes in plugins, etc); however I am now stuck with how to replace transformEvents with on<Event> with an EventTransformer.

I am presented with the following three errors for the code (at the end).

'LoginBloc.transformEvents' ('Stream<LoginState> Function(Stream<LoginEvent>, Stream<LoginState> Function(LoginEvent))') isn't a valid override of 'Bloc.transformEvents' ('Stream<Transition<LoginEvent, LoginState>> Function(Stream<LoginEvent>, Stream<Transition<LoginEvent, LoginState>> Function(LoginEvent))').

A value of type 'Stream<Transition<LoginEvent, LoginState>>' can't be returned from the method 'transformEvents' because it has a return type of 'Stream<LoginState>'.

The argument type 'Stream<LoginState> Function(LoginEvent)' can't be assigned to the parameter type 'Stream<Transition<LoginEvent, LoginState>> Function(LoginEvent)'.

I have done the normal thing of checking the documentation and examples for the bloc Dart Package, but unfortunately it is not terribly clear as to the way in which it should be implemented to bring it to the current standard (or at least not clear enough for myself to understand).

The below is a minimum reproducible example of code that is causing me grief.

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:meta/meta.dart';
import 'package:pawzed/repositories/userRepository.dart';
import 'package:rxdart/rxdart.dart';

part 'login_event.dart';
part 'login_state.dart';

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  UserRepository _userRepository;

  LoginBloc({required UserRepository userRepository})
      : //assert(userRepository != null), //will always be null
        _userRepository = userRepository,
        super(LoginState.empty()); //added LoginState.empty()

  LoginState get initialState => LoginState.empty();

  @override
  Stream<LoginState> transformEvents(
    Stream<LoginEvent> events,
    Stream<LoginState> Function(LoginEvent event) next,
  ) {
    final nonDebounceStream = events.where((event) {
      return (event is! EmailChanged || event is! PasswordChanged);
    });

    final debounceStream = events.where((event) {
      return (event is EmailChanged || event is PasswordChanged);
    }).debounceTime(Duration(milliseconds: 300));

    return super.transformEvents(
      nonDebounceStream.mergeWith([debounceStream]),
      next,
    );
  }
0 Answers
Related