ERROR: The argument type 'Object?' can't be assigned to the parameter type 'String?'
ERROR: The argument type 'Object?' can't be assigned to the parameter type 'String?'
我有一个 TextFormField 小部件包裹在 StreamBuilder 中,在 TextFormField 小部件中,在装饰中,当将 snapshot.error 传递给 errorText 参数时,它给出了一个错误:
参数类型'Object?'无法赋值给参数类型'String?'
这是带有表单和 TextFormField
的状态 class 的代码
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;
}
}
我的区块代码class:
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:
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()
errorText: snapshot.hasError ? snapshot.error.toString() : "",
这将在将其转换为字符串之前检查是否存在错误。如果没有错误,它也会防止运行时空异常。
有趣的是,没有人知道解决方案,因为他们没有遇到过这个问题。
您只需删除 InputDecoration() 之前的 const 关键字,
下面的代码将无法运行,因为它有 const
关键字 -
TextFormField(
decoration: const InputDecoration(
labelText: 'Email', errorText: snapshot.error),
keyboardType: TextInputType.emailAddress,
onChanged: bloc.changeEmail,
);
但是这段代码可以工作,因为它没有 const
关键字 -
TextFormField(
decoration: InputDecoration(
labelText: 'Email', errorText: snapshot.error),
keyboardType: TextInputType.emailAddress,
onChanged: bloc.changeEmail,
);
我有一个 TextFormField 小部件包裹在 StreamBuilder 中,在 TextFormField 小部件中,在装饰中,当将 snapshot.error 传递给 errorText 参数时,它给出了一个错误:
参数类型'Object?'无法赋值给参数类型'String?'
这是带有表单和 TextFormField
的状态 class 的代码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;
}
}
我的区块代码class:
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:
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()
errorText: snapshot.hasError ? snapshot.error.toString() : "",
这将在将其转换为字符串之前检查是否存在错误。如果没有错误,它也会防止运行时空异常。
有趣的是,没有人知道解决方案,因为他们没有遇到过这个问题。
您只需删除 InputDecoration() 之前的 const 关键字,
下面的代码将无法运行,因为它有 const
关键字 -
TextFormField(
decoration: const InputDecoration(
labelText: 'Email', errorText: snapshot.error),
keyboardType: TextInputType.emailAddress,
onChanged: bloc.changeEmail,
);
但是这段代码可以工作,因为它没有 const
关键字 -
TextFormField(
decoration: InputDecoration(
labelText: 'Email', errorText: snapshot.error),
keyboardType: TextInputType.emailAddress,
onChanged: bloc.changeEmail,
);