其他分享
首页 > 其他分享> > Flutter Bloc Pattren Stream Transformer对象导致语法错误

Flutter Bloc Pattren Stream Transformer对象导致语法错误

作者:互联网

我是Flutter和Dart语言的新手.在继续学习本教程的同时,我创建了一个具有2个StreamTransformers的验证器类,这是在尝试对batt pattren进行的,在该类中,用户将在2个TextField中键入电子邮件和密码,因此每次文本更改时都会进行验证.

如果我使用传入的电子邮件或密码,则会识别大量错误,特别是在FromHandler构造函数中的验证器类中,这些错误会被识别,但是像我在此处一样将它们放入if语句中时,它们不会重新识别为字符串对象,也就是说您可以在下面的屏幕截图中看到此处的错误原因.

validator file errors

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

bloc file errors

颤振版本:0.5.1
飞镖版本:2.0

for full code please have a look inside the login_stateful_bloc folder in this repo : 07002

解决方法:

只需删除=>这样表达和使用:

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

=> expr语法是{return expr; }. =>符号有时称为胖箭头语法.

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.

如果要使用=> ,尝试这样的事情:

   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!');
        }

     }

我在方法上使用静态方法,因为只能从初始化程序访问静态方法.

标签:dart,reactive-programming,flutter,react-state-management,android
来源: https://codeday.me/bug/20191108/2009356.html