我正在尝试从电话号码设置用户名,但收到“空”错误。从 Register 类中的警报对话框(用户在其中输入电话号码),在 onChanged 方法中,我调用 RegisterViewModel 以将值设置为用户名。
showAlertDialog(BuildContext context) async {
RegisterViewModel viewModel;
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(Languages.of(context).mustPhone),
content: TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
keyboardType: TextInputType.numberWithOptions(),
decoration: InputDecoration(hintText: Languages.of(context).phoneNum),
onChanged: (String value) {
setState(() {
viewModel.setName(value);
});}),
actions: <Widget>[
new TextButton(
child: new Text(Languages.of(context).register),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => OTPScreen(_textFieldController.text)));
}
)
],
);
}
);
}
RegisterViewModel 类中的方法是:
setName(val) {
username = val;
notifyListeners();
}
如何正确操作?提前致谢!