type 'BehaviorSubject<dynamic>' is not a subtype of type 'Stream<String>' of 'stream'

Issue

I have this weird error I’m getting and I can’t seem to fix this. I am implementing the bloc pattern in my login page. I can’t seem to point out what I am doing wrong. Here is my code

login_bloc.dart

import 'package:rxdart/rxdart.dart';
import 'dart:async';

class LoginBloc extends Object with Validators {
  final _emailController = BehaviorSubject<dynamic>();
  final _passwordController = BehaviorSubject<dynamic>();

  Function(String) get emailChanged => _emailController.sink.add;
  Function(String) get passwordChanged => _passwordController.sink.add;

  Stream<String> get emailValidator => _emailController.stream.transform(emailValidators);
  Stream<String> get passwordValidator =>  _passwordController.stream.transform(passwordValidators);

  Stream<bool> get submitCheck => Rx.combineLatest2(emailValidator, passwordValidator, (e, p) => true);

  dispose() {
   _emailController?.close();
   _passwordController?.close();
 }
}

mixin Validators {
  var emailValidators =
  StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
   if (email.contains("@")) {
     sink.add(email);
   } else {
     sink.addError("Email is not valid");
   }
});

  var passwordValidators = StreamTransformer<String, String>.fromHandlers(
     handleData: (password, sink) {
    if (password.length > 0) {
      sink.add(password);
    } else {
      sink.addError("Password Field should not be empty");
    }
 });
}

final loginBloc = LoginBloc();

here is the widget i created calling the bloc

  Widget _emailPasswordWidget() {
return Column(
  children: <Widget>[
    Container(
  margin: EdgeInsets.symmetric(vertical: 10),
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Text(
        'Email ID',
        style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
      ),
      SizedBox(
        height: 10,
      ),
      StreamBuilder<String>(
        stream: loginBloc.emailValidator,
        builder: (context, snapshot) {
          return TextField(
              onChanged: loginBloc.emailChanged,
              obscureText: false,
              decoration: InputDecoration(
                  errorText: snapshot.error,
                  border: InputBorder.none,
                  fillColor: Color(0xfff3f3f4),
                  filled: true));
        }
      )
    ],
  ),
)
   loginBloc.passwordChanged),
  ],
);
}

I would appreciate any help whatsoever. I have checked online and I have not seen any help. I am using rxdart version ^0.23.1. I have used a streamcontroller instead of the behaviour subject and I have used a publishsubject too and i still get the error.

Solution

there is no need for the BehaviorSubject to be of type dynamic when everything going through will be Strings, so the best fix is to type it like so :

BehaviorSubject<String>()

Answered By – Benoit Jadinon

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *