4

I am new to Flutter and Dart language. while following along a tutorial I create a validator class which has 2 StreamTransformers, this is being done in an attempt for bloc pattren, where user will type email and password in 2 TextFields and hence will be validated everytime the text changes in them.

And I am getting bulk of errors specifically in that validator class within the FromHandler constructor if I use incoming email or password they are recognized but when I put them within an if statement like I have done here they are not recignized as string objects and that is the cause of error here as you can see in the screenshot below.

validator file errors

apart from them bloc file also has a couple of errors as shown in 2nd screenshot below:

bloc file errors

Flutter version : 0.5.1 dart version : 2.0

for full code please have a look inside the login_stateful_bloc folder in this repo : https://github.com/touseefbsb/LoginStateful

Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75

1 Answers1

5

Just remove the => expression and use like this way:

    final validateEmail = StreamTransformer<String,String>.fromHandlers(
        handleData: (email,sink) {
          if(email.contains('@')){
            sink.add(email);
          }
          else{
            sink.addError('Email not valid!');
          }
        }
       );

The => expr syntax is a shorthand for { return expr; }. The => notation is sometimes referred to as fat arrow syntax.

Note: Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;). For example, you can’t put an if statement there, but you can use a conditional expression.

If you want to use => , try something like this:

   final validateEmail = StreamTransformer<String,String>.fromHandlers(
      handleData: (email, sink) => _fatArrowMethod(email,sink));

     static _fatArrowMethod(email, sink){
       if(email.contains('@')){
          sink.add(email);
        }
        else{
          sink.addError('Email not valid!');
        }

     }

I'm using static on the method because only static methods are accessed from the initializer.

diegoveloper
  • 93,875
  • 20
  • 236
  • 194