johnwargo
johnwargo

Reputation: 787

Flutter TextField "Changing the content within the the composing region may cause the input"

I created a new Flutter app and I'm trying to set an initial value in a TextField widget using a process I've used before in several apps. Whenever I edit content in the field, the Android Studio Run window displays:

W/TextInputPlugin(18696): Changing the content within the the composing region may cause the input method to behave strangely, and is therefore discouraged. See https://github.com/flutter/flutter/issues/78827 for more details
W/IInputConnectionWrapper(18696): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(18696): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(18696): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(18696): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(18696): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(18696): endBatchEdit on inactive InputConnection

When I look at the referenced link, I don't see anything there that describes the solution.

To test this, I made a new, clean app with just a Text widget and the TextEdit widget and I still get the same issue. I'm not doing anything except setting the default value. Ultimately I want to capture changes to the input field and write the value to local storage, but I thought I'd fix this input error first.

Here's the code for my test app:

import 'package:flutter/material.dart';

const appName = 'TextField Test';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: appName,
      debugShowCheckedModeBanner: false,
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      home: HomePage(title: appName),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late TextEditingController textEditingController;
  String _inputValue = '';

  @override
  void initState() {
    super.initState();
    textEditingController = TextEditingController(text: _inputValue);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Padding(
          padding: const EdgeInsets.all(10.0),
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Text('This is some content in a Text widget'),
            SizedBox(height: 10),
            TextField(
              autofocus: true,
              decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'Enter some text here'),
              controller: textEditingController,
            ),
          ]),
        ));
  }
}

Can someone please tell me what I'm doing wrong?

When I add a listener to the controller, I noticed that its firing multiple times for every character I type:

I/flutter (18696): Listener fired the e
I/flutter (18696): Listener fired the e
I/flutter (18696): Listener fired the en
I/flutter (18696): Listener fired the en

Here's the listener code:

 @override
  void initState() {
    super.initState();
    textEditingController = TextEditingController(text: _inputValue);
    textEditingController.addListener(() {
      print('Listener fired ${textEditingController.text}');
    });
  }

Upvotes: 4

Views: 13511

Answers (7)

Mixron469
Mixron469

Reputation: 1

This is my solution. Disable all settings in Gboard setting > Text correction.

https://github.com/siqwin/mask_text_input_formatter/issues/64#issuecomment-1327205058

Upvotes: 0

Canilho
Canilho

Reputation: 1209

I run into the same message, and this is how I solved it:

Inside your TextFormField I've added the property keyboardType: TextInputType.text, Change it accordingly to your input type (ex: name, number, text, phone, etc...)

After that, I've added the function onEditingComplete: () {<your code here>}, This allows you to enable any future action after the edition of the text, like changing focus, submitting the form, etc. Full example:

TextFormField(
    autofocus: true,
    decoration: InputDecoration(
        border: OutlineInputBorder(),
        hintText: 'Enter some text here'),
    controller: textEditingController,
    keyboardType: TextInputType.text,
    onEditingComplete: () {
        print(textEditingController.text);
    },
),

Upvotes: 0

Ian Nato
Ian Nato

Reputation: 1037

on my side, the issue came about when I declared the textEditingController inside the widget build function like this

Wrong declaration of textEditingController

 @override
 Widget build(BuildContext context) {

   TextEditingController textEditingController = TextEditingController();


return TextField(
  controller: textEditingController ,
  decoration: InputDecoration(

      hintText: "hintText",
      labelText: "placeholder",

  ),
  keyboardType: TextInputType.name,
  onChanged: (text){
    
  },

);  }

This issue was sorted when I declared the textEditingController outside the widget function like this

Right declaration of textEditingController

TextEditingController textEditingController = TextEditingController();

@override
Widget build(BuildContext context) {
return TextField(
  controller: textEditingController ,
  decoration: InputDecoration(

    hintText: "hintText",
    labelText: "placeholder",

 ),
 keyboardType: TextInputType.name,
onChanged: (text){

});
}

also when setting the textEditingController.text I would recommend setting it in the initState function rather than in the widget build function as shown below

@override
void initState() {
  super.initState();

  textEditingController.text = someValueVariable;
}

I hope this helps.

Upvotes: 0

Willy Hystor
Willy Hystor

Reputation: 21

So I met with the same 'problem' here.

I used TextFormField widget to be my input. These logs come when I don't define the keyboardType props for simple label input and start typing random inputs.

The soulution I found is when I define keyboardType: TextInputType.name, the logs gone for good. I also tested it with keyboardType: TextInputType.email and keyboardType: TextInputType.password, it works on both.

Hope it helps.

Upvotes: 2

Eslam Shaban
Eslam Shaban

Reputation: 44

try add static keyword .

 static late TextEditingController textEditingController;

Upvotes: 1

This is just a warning. İf it bothers you don't use computer keyboard, instead use virtual keyboard on the simulator.

Upvotes: 4

johnnycopes
johnnycopes

Reputation: 424

Unfortunately, this looks like a longstanding issue that has yet to be resolved by the Flutter team according to this thread: https://github.com/flutter/flutter/issues/9471. I'm running into the same issue.

Upvotes: 2

Related Questions