在构建LoginPage(dirty,state:_LoginPageState#bf00d)时引发了以下断言:
必须为文本窗口小部件提供非空字符串。
'package:flutter / src / widgets / text.dart':
断言失败:第360行pos 10:'data!= null'
我有此错误,在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 In\n\nUser id: $userId';
});
widget.onSignIn();
}
catch (e) {
setState(() {
_authHint = 'Sign In Error\n\n${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)
);
}
关于android - 登录页面状态失败的断言问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63654800/