【发布时间】:2021-07-09 08:48:04
【问题描述】:
我遇到了这种错误,希望有人能告诉我如何修复它。
代码:
import 'package:flutter/material.dart';
import 'package:login_form/model/login_model.dart';
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final scaffoldKey = GlobalKey<ScaffoldState>();
GlobalKey<FormState> globalFormKey = new GlobalKey<FormState>();
bool hidePassword = true;
LoginRequestModel requestModel;
@override
void initState() {
super.initState();
requestModel = new LoginRequestModel();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
backgroundColor: Theme.of(context).accentColor,
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
Container(
width: double.infinity,
padding: EdgeInsets.symmetric(vertical: 30, horizontal: 20),
margin: EdgeInsets.symmetric(vertical: 85, horizontal: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Theme.of(context).primaryColor,
boxShadow: [
BoxShadow(
color: Theme.of(context).hintColor.withOpacity(0.2),
offset: Offset(0, 10),
blurRadius: 20)
],
),
child: Form(
key: globalFormKey,
child: Column(
children: <Widget>[
SizedBox(
height: 25,
),
Text(
"Login",
style: Theme.of(context).textTheme.headline2,
),
SizedBox(
height: 20,
),
new TextFormField(
keyboardType: TextInputType.emailAddress,
onSaved: (input) => requestModel.email = input,
validator: (input) =>
!(input?.contains("@") ?? false)
? "Email id Should be valid"
: null,
decoration: new InputDecoration(
hintText: "Email Address",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentColor
.withOpacity(0.2),
)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).accentColor)),
prefixIcon: Icon(Icons.email,
color: Theme.of(context).accentColor),
),
),
SizedBox(
height: 20,
),
new TextFormField(
keyboardType: TextInputType.text,
onSaved: (input) => requestModel.password = input,
validator: (input) => (input != null &&
input.length < 3)
? "Password should be more than < 3 characters"
: null,
obscureText: hidePassword,
decoration: new InputDecoration(
hintText: "Password",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentColor
.withOpacity(0.2),
)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).accentColor)),
prefixIcon: Icon(Icons.lock,
color: Theme.of(context).accentColor),
suffixIcon: IconButton(
onPressed: () {
setState(() {
hidePassword = !hidePassword;
});
},
icon: Icon(hidePassword
? Icons.visibility_off
: Icons.visibility),
),
),
),
SizedBox(
height: 30,
),
TextButton(
child: Text(
"Login",
style: TextStyle(color: Colors.white),
),
onPressed: () {
if (validateAndSave()) {
print(requestModel.toJson());
}
},
style: TextButton.styleFrom(
padding: EdgeInsets.symmetric(
vertical: 12,
horizontal: 80,
),
backgroundColor:
Theme.of(context).accentColor,
shape: StadiumBorder())),
],
),
),
),
],
)
],
),
));
}
bool validateAndSave() {
final form = globalFormKey.currentState;
if (form!.validate()) {
form.save();
return true;
}
return false;
}
}
这是我的 Loginmodel.dart 代码
class LoginResponseModel {
final String status;
final String message;
final String kid;
final String uid;
final String email;
final String password;
LoginResponseModel({
required this.status,
required this.message,
required this.kid,
required this.uid,
required this.email,
required this.password});
factory LoginResponseModel.fromJson(Map<String, dynamic> json) {
return LoginResponseModel(
status: json["status"] as String? ?? "",
message: json["message"] as String? ?? "",
kid: json["kid"] as String? ?? "",
uid: json["uid"] as String? ?? "",
email: json["email"] as String? ?? "",
password: json["password"] as String? ?? "",
);
}
}
class LoginRequestModel {
String email;
String password;
LoginRequestModel({
required this.email,
required this.password,
});
Map<String, dynamic> toJson() {
Map<String, dynamic> map = {
'email': email.trim(),
'password': password.trim(),
};
return map;
}
}
错误:
LoginRequestModel requestModel 上的错误;说“必须初始化非 Nullable 实例字段‘requestModel’。
然后 requestModel = new LoginRequestModel() 上的错误消息说 The Named Parameter 'email is required,但没有相应的参数。
并且 Last On OnSaved = 输入说“'String 类型的值?'不能分配给“字符串”类型的变量。”
非常感谢您给出的答案
【问题讨论】:
标签: visual-studio flutter dart