【问题标题】:ERROR: The argument type 'Object?' can't be assigned to the parameter type 'String?'错误:参数类型“对象?”不能分配给参数类型“字符串?”
【发布时间】:2021-10-10 19:03:43
【问题描述】:

我有一个 TextFormField 小部件包装在 StreamBuilder 中,在 TextFormField 小部件中,在装饰内,当将 snapshot.error 传递给 errorText 参数时,它会给出错误:

参数类型“对象?”不能赋值给参数类型'String?'

这是带有表单和 TextFormField 的状态类的代码

class LoginScreen extends StatefulWidget{
  State<StatefulWidget> createState() {
    return _LoginScreen();
  }
}

class _LoginScreen extends State<LoginScreen>{
  final form_key = GlobalKey<FormState>();

  Widget build(context){
    return Container(
      margin: EdgeInsets.all(20),

      child: Form(
        key: form_key,

        child: Column(
          children: [
            emailField(),
            passwordField(),
            Padding(
              padding: EdgeInsets.all(7),

              child: submitButton(),
            ),
            Padding(
              padding: EdgeInsets.all(7),

              child: ResetButton(),
            )
          ],
        ),
      ),
    );
  }

  Widget emailField(){
    return StreamBuilder(
      stream: bloc.email,

      builder: (context, snapshot){
        return TextFormField(
          decoration: const InputDecoration(
            labelText: 'Email',

            errorText: snapshot.error,
          ),

          keyboardType: TextInputType.emailAddress,

          onChanged: bloc.changeEmail,
        );
      },
    );
  }

  Widget passwordField(){
    return StreamBuilder(
      stream: bloc.pass,

      builder: (context, snapshot){
        return TextFormField(
          decoration: const InputDecoration(
            labelText: 'Password'
            
            errorText: snapshot.error,
          ),

          obscureText: true,
        );
      },
    );
  }

  Widget submitButton(){
    return ElevatedButton(
      child: Text('SUBMIT'),

      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.resolveWith(getColor),
      ),

      onPressed: (){},
    );
  }

  Widget ResetButton(){
    return ElevatedButton(
      child: Text('RESET'),

      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.resolveWith(getColor),
      ),
      
      onPressed: (){
        form_key.currentState!.reset();
      }
    );
  }  

  Color getColor(Set<MaterialState> states) {
    const Set<MaterialState> interactiveStates = <MaterialState>{
      MaterialState.pressed,
      MaterialState.hovered,
      MaterialState.focused,
    };
    if (states.any(interactiveStates.contains)) {
      return Colors.orange.shade600;
    }
    return Colors.blue.shade400;
  }
}

我的 bloc 类的代码:

class Bloc with Validators{
  final _email = StreamController<String?>();
  final _pass = StreamController<String?>();

  //get access to stream

  Stream<String?> get email => _email.stream.transform(validate_email);
  Stream<String?> get pass => _pass.stream.transform(validate_password);

  //change new data

  Function(String?) get changeEmail => _email.sink.add;
  Function(String?) get changePass => _pass.sink.add;

  dispose(){
    _email.close();
    _pass.close();
  }
}

这里是验证器类:

class Validators{
  final validate_email = StreamTransformer<String?, String?>.fromHandlers(
    handleData: (String? email, sink){
      if(email!.contains('@')){
        sink.add(email);
      }else{
        sink.addError('Enter valid email');
      }
    }
  );

  final validate_password = StreamTransformer<String?, String?>.fromHandlers(
    handleData: (String? pass, sink){
      if(pass!.length < 4){
        sink.addError('Enter valid password');
      }else{
        sink.add(pass);
      }
    }
  );
}

【问题讨论】:

  • 尝试使用snapshot.error?.toString();"${snapshot.error}"

标签: flutter dart flutter-streambuilder


【解决方案1】:
errorText: snapshot.hasError ? snapshot.error.toString() : "",

这将在将其转换为字符串之前检查是否有错误。如果没有错误,它也会阻止运行时空异常。

【讨论】:

  • 它仍然返回一个空字符串,因此它在带有空字符串的文本字段上显示一个红色错误。相反,根据另一个答案,使用 snapshot.error?.toString() 效果更好。谢谢你的回答。
【解决方案2】:

你需要使用

snapshot.error?.toString()

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 2021-07-17
    • 2021-12-14
    • 1970-01-01
    • 2021-06-19
    • 2021-12-20
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多