首页 > 解决方案 > 为什么 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 架构。

谢谢你的帮助。

标签: androidfluttercrashtextfield

解决方案


推荐阅读