非活动 InputConnection 上的 getSelectedText 颤动

Abh*_*ith 20 dart flutter flutter-layout

我有一个奇怪的问题,当我尝试在文本字段中输入值时,当我尝试键入时键盘出现了当我尝试输入年龄、身高、体重键盘并在几秒钟内出现时,这是我收到的错误/警告,颤振医生没有问题,

W/IInputConnectionWrapper(23904): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(23904): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper( 8756): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper( 8756): endBatchEdit on inactive InputConnection
Run Code Online (Sandbox Code Playgroud)

代码和视图

class RegistrationScreenState extends State<RegistrationScreen> {

 TextEditingController mobile = TextEditingController();
  TextEditingController Username = TextEditingController();
  TextEditingController Place = TextEditingController();
  TextEditingController age = TextEditingController();
  TextEditingController height = TextEditingController();
  TextEditingController weight = TextEditingController();

  void initState() {
    getDetails();

  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery
        .of(context)
        .size;
    return Scaffold(
      appBar: AppBar(
        title: Text("Registration", style: TextStyle(color: Colors.black)),
        backgroundColor: Colors.orange,
      ),
      body: SingleChildScrollView(
        child: Stack(
          children: [
            Column(
              children: [
                Container(
                  child: Image.asset('assets/images/gym.png',
                      height: 150,
                      width: double.infinity,
                      fit: BoxFit.fitWidth),
                ),
                SizedBox(
                  height: 50,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          controller: Username,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.perm_identity),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          controller: mobile,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.mobile_screen_share),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "User Information",
                        style: TextStyle(
                            fontSize: 15, fontWeight: FontWeight.bold),
                      )),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          controller: Place,
                          decoration: InputDecoration(),
                        ),

                      ),
                    ),
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          controller: age,
                          decoration: InputDecoration(
                            hintText: 'Age',
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          controller: height,
                          decoration: InputDecoration(
                            hintText: 'Height(in cm)',
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: TextField(
                          controller: weight,
                          decoration: InputDecoration(
                            hintText: 'Weight(in kg)',
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }


  //  get & fill Shareprefrece data to textfield
  getDetails() async {
    Future user = SharedPrefrence().getUserMobile();
    Future name = SharedPrefrence().getUserName();
    Future place = SharedPrefrence().getUserPlace();

    user.then((data) async {
      var mobile_no = data;
      setState(() {
        if (mobile_no.isNotEmpty) {
          mobile.text = mobile_no;
        }
        else
          {
            mobile.text = "";
          }
      });
    });

    name.then((data) async {
      var user_name = data;
      setState(() {
        if (user_name.isNotEmpty) {
          Username.text = user_name;
        }
      });
    });

    place.then((data) async {
      var user_place = data;
      setState(() {
        if (user_place.isNotEmpty) {
          Place.text = user_place;
        }
      });
    });
  }



}
Run Code Online (Sandbox Code Playgroud)

小智 13

我不确定这是否会帮助你,但我也面临同样的问题。当我在 Form 中使用 TextFormField 时出现此错误,问题是我在 build 方法中创建了全局表单键。

class _AddTaskState extends State<AddTask> {
final _formKey = GlobalKey<FormState>(); // <-
String name;
String description;
@override
Widget build(BuildContext context) {
 // first I was using that here.
return Scaffold(
Run Code Online (Sandbox Code Playgroud)

  • 不,它不起作用,我之前尝试过@Aashar Wahla (2认同)
  • 就我而言,我在一个无状态小部件中创建了用于状态的密钥,将密钥移动到状态本身并将其传递给小部件就达到了目的 (2认同)

Abh*_*ith 7

已知问题,在此处跟踪,它仍然存在。没有解释是什么原因造成的。

我做了一些挖掘。看起来 Android 生成这些警告是因为我们在引擎的 TextInputPlugin 中错误地保存了 InputConnection。但我还没有真正弄清楚我们做错了什么。

来源


Nai*_*bir 6

如果您使用表单验证器,请将表单键移出构建上下文。这应该可以解决您的问题。

代替 -

class SingInScreen extends StatelessWidget {
final _sigInFormKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    Size size = getRelativeSize(context);
Run Code Online (Sandbox Code Playgroud)

用这个-

`final _sigInFormKey = GlobalKey<FormState>();

class SingInScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Size size = getRelativeSize(context);`
Run Code Online (Sandbox Code Playgroud)


Abe*_*hun 5

就我而言,我TextFeildAnimatedSwitcher. 问题是key: UniqueKey()我的小部件中包含TextFeild. 因此,当我触摸文本 feid 时,它UniqueKey()会被调用,然后小部件树会重新生成。