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}');
    });
  }
                This is just a warning. İf it bothers you don't use computer keyboard, instead use virtual keyboard on the simulator.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With