flutter - BloC 的 TextEditingController 异常
问题描述
有人可以帮帮我吗?
我不明白为什么它会给出这个例外:
“处理后使用了 TextEditingController。”
按照我的逻辑,它不应该给我这个例外。
这是有问题的课程:
class _MobileLayoutState extends State<_MobileLayout> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController _textEditingController;
@override
void initState() {
super.initState();
_textEditingController = TextEditingController();
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(builder: (context, constraints) {
return FractionallySizedBox(
widthFactor: 1.0,
heightFactor: 1.0,
child: Stack(
alignment: Alignment.center,
children: [
Column(
children: [
_buildSelectButton(
context,
"delivery_background.jpg",
S.of(context).orderDeliverySelectString,
constraints, onPressed: () {
orderType = OrderType.Delivery;
}),
_buildSelectButton(
context,
"restaurant_background.jpg",
S.of(context).orderRestaurantSelectString,
constraints, onPressed: () {
orderType = OrderType.Restaurant;
_showTableSelect(context);
}),
],
),
_CenterWidget(),
],
),
);
}),
);
}
_showTableSelect(BuildContext context) async {
await showCustomRoundedDialog(
context,
title: S.of(context).tableSelectDialogTitle,
body: Form(
autovalidate: true,
key: _formKey,
child: TextFormField(
controller: _textEditingController,
decoration: InputDecoration(
icon: Icon(Icons.restaurant_menu),
hintText: S.of(context).tableSelectDialogTitle,
),
keyboardType: TextInputType.number,
validator: (String value) {
return (value.isEmpty || !Validators.isNumber(value))
? S.of(context).tableSelectDialogErrorMsg
: null;
},
),
),
actions: [
Flexible(
child: GestureDetector(
child: Container(
width: sv(300),
child: Text(
"Ok",
style: bigTitleStyle.copyWith(
color: Colors.white,
),
textAlign: TextAlign.center,
),
),
onTap: () {
if (_formKey.currentState.validate()) {
if (mounted) {
currentTable = int.parse(_textEditingController.text.trim());
}
Navigator.pop(context);
}
},
),
),
],
);
if (currentTable != null) {
BlocProvider.of<MainNavigationBloc>(context)
.add(MainNavigationEvent.ProdCategoryShown);
}
}
Widget _buildSelectButton(BuildContext context, String imageName, String text,
BoxConstraints constraints,
{Function onPressed}) {
return GestureDetector(
onTap: onPressed,
child: Container(
width: constraints.maxWidth,
height: (constraints.maxHeight * 0.5),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("$imageAssetsPath/$imageName"),
fit: BoxFit.cover),
),
child: Container(
color: Colors.black45.withAlpha(120),
child: Center(
child: Text(
text,
style: bigTitleStyle.copyWith(color: Colors.white),
),
),
),
),
);
}
}
弹出对话框后我没有使用控制器,那么为什么它会给我这个异常呢?
该应用程序是使用带有 flutter_bloc 包的 BloC 模式开发的。
- 构建系统:
- Flutter Channel 测试版,1.20.0-7.3.pre
- 安卓 SDK 版本 29.0.3
- Ubuntu 20.04
解决方案
推荐阅读
- python - 熊猫:从数据框中选择系列会丢失列级名称的信息
- vba - 随机改变形状的位置
- html - 即使语法正确,Angular 中的货币过滤器也会向我显示一个空格。谁能帮我吗?
- php - 如何在php api中显示unicode数据
- sql - SSIS 动态连接管理器中的错误处理
- tensorflow - 寻找有关如何为 CNN 设置参数的建议(顺序)
- google-apps-script - 使用 GAS 删除 Google 幻灯片的所有超链接
- php - 负后向正则表达式在 PHP 中不起作用
- python - 试图从 Python 中的字典列表中提取特定数据。(使用罗宾股票)
- latex - Latex 中的目录