登录页面状态的断言问题失败



以下断言是在构建LoginPage时抛出的(dirty,state:_LoginPageState#bf00d(:必须向Text小部件提供非null字符串。'package:flutter/src/widgets/text.dart':断言失败:第360行位置10:"data!="空

我有这个错误,看起来在text.dart.中没有问题

这是我的loginPage.dart页面。

我找不出问题。

import 'package:flutter/material.dart';
import 'package:sporprojesi/login/primary_button.dart';
import 'package:sporprojesi/login/signin.dart';

class LoginPage extends StatefulWidget {
LoginPage({Key key, this.title, this.auth, this.onSignIn}) : super(key: key);
final String title;
final BaseAuth auth;
final VoidCallback onSignIn;
@override
_LoginPageState createState() => new _LoginPageState();
}
enum FormType {
login,
register
}
class _LoginPageState extends State<LoginPage> {
static final formKey = new GlobalKey<FormState>();
String _email;
String _password;
FormType _formType = FormType.login;
String _authHint = '';

@override
void initState() {
super.initState();}
bool validateAndSave() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
return true;
}
return false;
}
void validateAndSubmit() async {
if (validateAndSave()) {
try {
String userId = _formType == FormType.login
? await widget.auth.signIn(_email, _password)
: await widget.auth.createUser(_email, _password);
setState(() {
_authHint = 'Signed InnnUser id: $userId';
});
widget.onSignIn();
}
catch (e) {
setState(() {
_authHint = 'Sign In Errornn${e.toString()}';
});
print(e);
}
} else {
setState(() {
_authHint = '';
});
}
}
void moveToRegister() {
formKey.currentState.reset();
setState(() {
_formType = FormType.register;
_authHint = '';
});
}
void moveToLogin() {
formKey.currentState.reset();
setState(() {
_formType = FormType.login;
_authHint = '';
});
}
List<Widget> usernameAndPassword() {
return [
padded(child: new TextFormField(
key: new Key('email'),
decoration: new InputDecoration(labelText: 'Email'),
autocorrect: false,
validator: (val) => val.isEmpty ? 'Email can't be empty.' : "",
onSaved: (val) => _email = val,
)),
padded(child: new TextFormField(
key: new Key('password'),
decoration: new InputDecoration(labelText: 'Password'),
obscureText: true,
autocorrect: false,
validator: (val) => val.isEmpty ? 'Password can't be empty.' : "",
onSaved: (val) => _password = val,
)),
];
}
List<Widget> submitWidgets() {
switch (_formType) {
case FormType.login:
return [
new PrimaryButton(
key: new Key('login'),
text: 'Login',
height: 44.0,
onPressed: validateAndSubmit
),
new FlatButton(
key: new Key('need-account'),
child: new Text("Need an account? Register"),
onPressed: moveToRegister
),
];
case FormType.register:
return [
new PrimaryButton(
key: new Key('register'),
text: 'Create an account',
height: 44.0,
onPressed: validateAndSubmit
),
new FlatButton(
key: new Key('need-login'),
child: new Text("Have an account? Login"),
onPressed: moveToLogin
),
];
}
return null;
}
Widget hintText() {
return new Container(
//height: 80.0,
padding: const EdgeInsets.all(32.0),
child: new Text(
_authHint,
key: new Key('hint'),
style: new TextStyle(fontSize: 18.0, color: Colors.grey),
textAlign: TextAlign.center)
);
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
backgroundColor: Colors.grey[300],
body: new SingleChildScrollView(child: new Container(
padding: const EdgeInsets.all(16.0),
child: new Column(
children: [
new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Container(
padding: const EdgeInsets.all(16.0),
child: new Form(
key: formKey,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: usernameAndPassword() + submitWidgets(),
)
)
),
])
),
hintText()
]
)
))
);
}
Widget padded({Widget child}) {
return new Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: child,
);
}
}

看起来_authHint为空

修改hintText方法,如下所示??使用了运算符。类似地,如果需要的话,您应该拥有其他文本小部件。

Widget hintText() {
return new Container(
//height: 80.0,
padding: const EdgeInsets.all(32.0),
child: new Text(
_authHint?? "",
key: new Key('hint'),
style: new TextStyle(fontSize: 18.0, color: Colors.grey),
textAlign: TextAlign.center)
);
}

最新更新