android - 为什么 TextEditingController 在某些设备上会崩溃?
问题描述
我有一个非常简单的应用程序,其中有一个用户输入电话号码的表单,该应用程序调用 API 以获取该号码在餐厅或商店下的订单。好吧,在某些设备上,一旦显示表单并且用户触摸文本字段,应用程序就会在没有任何警告的情况下崩溃,只是离开屏幕。这不会在各种模拟器中发生,也不会在大量设备中发生,但在其中一些设备中会发生(不幸的是,其中一个是为应用程序付费的人......)。这是表单的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nascenterio/dialogs.dart';
import '../global.dart';
class PhoneScreen extends StatefulWidget {
PhoneScreen({Key key}) : super(key: key);
@override
_PhoneScreenState createState() => _PhoneScreenState();
}
class _PhoneScreenState extends State<PhoneScreen> {
final TextEditingController myController = TextEditingController();
FocusNode phonefocusNode;
String myPhone;
@override
void initState() {
super.initState();
myController.text = glbPhone // global variable to init the field value;
phonefocusNode = FocusNode();
phonefocusNode.requestFocus() ;
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
phonefocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: glbAppBarColor,
title: Text("Meus Pedidos"),
),
body:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center ,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: myController,
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
textInputAction: TextInputAction.search,
focusNode: phonefocusNode,
autofocus: true,
onEditingComplete: () {
if (myController.text.length >= 8) {
Navigator.pop(context, myController.text);
} else {
aviso(context,"O número do telefone deve ter pelo menos 8 dígitos.");
}
},
decoration: InputDecoration(
hintText: 'Número do telefone (pelo menos 8 dígitos)',
),
),
),
MaterialButton(
color: glbAppBarColor,
child: Text("Pesquisar",
style: TextStyle( color: Colors.white),) ,
onPressed: () {
if (myController.text.length >= 8) {
Navigator.pop(context, myController.text);
} else {
aviso(context,"O número do telefone deve ter pelo menos 8 dígitos.");
}
},)
],
),
)
);
}
}
可以看出,这个表单没有访问 API,它只是返回在 TextField 中键入的数字。我不是 Flutter 专家,也许我应该使用一些配置或参考来解决这个问题。顺便说一句,如果有帮助的话,其中一款应用程序崩溃的手机具有 x86-64 架构。
谢谢你的帮助。
解决方案
推荐阅读
- node.js - 按文档之间常见数组项的数量对 mongo 查找进行排序
- r - 在R图中分配大量(10-15)视觉上非常不同的颜色的最佳方法是什么(对于曲线 - 特别是CDP)
- python - 如何在 Python 中将文本字符串编码为数字?
- go - 字符串迭代编程
- c# - CreateDirectConversation/CreateDirectConversationAsync Bot Framework 4 中的 ArgumentNullException
- python - 在 Mayavi 中为单个补丁着色
- html - 制作简单的静态网站多语言的选项有哪些?
- excel-formula - 如果一个数字,则删除最后一个值,否则什么都不做
- assembly - 如何在 MIPS 中通过 $a0 传递浮点数?
- sql - 为什么这个双重否定查询与肯定查询不同?