Flutter: getTextBeforeCursor on inactive InputConnection
Asked Answered
M

5

13

I'm trying to retrieve the user input inside a TextField title so it can be passed to a function called void _filterList(value). However, everytime I put some text this errors appear:

W/IInputConnectionWrapper( 7715): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 7715): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 7715): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper( 7715): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper( 7715): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 7715): endBatchEdit on inactive InputConnection

This is my code:

List filteredlist = [];
List entries = [];
bool isSearching = false;

getCountries() async {
  var response =
    await Dio().get('https://restcountries.eu/rest/v2/regionalbloc/eu');
return response.data;
}

@override
void initState() {
getCountries().then((data) {
  setState(() {
    entries = filteredlist = data;
  });
});
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.blue,
      title: !isSearching
          ? Text('All EU Countries')
          : TextField(
              onChanged: (value) {
                _filterList(value);
              },
              style: TextStyle(color: Colors.white),
              decoration: InputDecoration(
                icon: Icon(Icons.search, color: Colors.white),
                hintText: "Search Here",
                hintStyle: TextStyle(color: Colors.white),
              )),
      actions: <Widget>[
        isSearching
            ? IconButton(
                icon: Icon(Icons.cancel),
                onPressed: () {
                  setState(() {
                    this.isSearching = false;
                    filteredlist = entries;
                  });
                },
              )
            : IconButton(
                icon: Icon(Icons.search),
                onPressed: () {
                  setState(() {
                    this.isSearching = true;
                  });
                })
      ],
    ),
    body: _buildList());
}

This is my function:

void _filterList(value) {
setState(() {
  filteredlist = entries.where(
      (entry) => entry['name'].toLoweCase().contains(value.toLowerCase()));
});

}

As far as I have undestrood there seems to be a problem with the keyboard, but I haven't figured out how to prevent it

Minutes answered 22/7, 2020 at 20:10 Comment(1)
What version of flutter are you using?Extant
S
7

Make sure the controllers of the textfields are out of the build method and make sure the widget is statefull

Shrunken answered 28/2, 2021 at 0:3 Comment(0)
U
2

Known issue, tracked here. Even this sample code gives warnings in android. No consensus on what's causing it.

I did a little bit of digging. It looks like Android is generating these warnings because we are holding the InputConnection incorrectly in the Engine's TextInputPlugin. I haven't really figured out what we're doing wrong, though.

source

Undistinguished answered 15/8, 2020 at 9:24 Comment(1)
I tend to get this warning when I type into any text field using my computer's keyboard rather than the on-screen mobile keyboard.Philps
O
1

You could do it using a TextEditingController and a FutureBuilder. Like this:

var searchController = TextEditingController();
var searchTerm = "";

  @override
  void initState() {
    super.initState();
    searchController.addListener(onSearch);
  }

 onSearch() {
    setState(() {
      searchTerm = searchController.text;
      updateList();
    });
  }

  Future updateList() async {
    return await getYourFilteredList(searchTerm);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(     
      body: Center(
        child: Column(
          children: <Widget>[
           TextField(controller: searchController),
            FutureBuilder(
      future: updateList(),
      builder: (context, snapshot) {
        if (snapshot.hasData)
          return Expanded(
            child: _buildList(snapshot.data),
          );
        return CircularProgressIndicator();
      },
    );
          ],
        ),
      ),
    );
  }

Note: I did not test this code, but I have written something similar.

Ortiz answered 20/8, 2020 at 22:39 Comment(0)
B
1

I encountered this in my project too. The cause of mine was I had a code invoking MediaQuery.of(context).height inside the build method of the class that returns the Scaffold Widget. I had to remove it and place it in the child widget class. For example:

Before I had...

```Class MyHomePage extends StatelessWidget {
     //some code
     Widget build (BuildContext context) {
       double availableHeight = MediaQuery.of(context).height - statusBarHeight - appBarHeight;
       return Scaffold (//some code that passes availableHeight to a widget's [MyHomePageBody] constructor);
     }
}```

Now, I have...

```Class MyHomePage extends StatelessWidget {
     //some code
     Widget build (BuildContext context) {
       
       return Scaffold (//some code);
     }
}

Class MyHomePageBody extends StatelessWidget {
     //some code
     Widget build (BuildContext context) {
       double availableHeight = MediaQuery.of(context).height - statusBarHeight - appBarHeight;
       return Container (//some code that uses availableHeight);
     }
}```

Some other fix that could work is to ensure your flutter channel is on the stable channel. If not, you can change the channel through the following command flutter channel stable

Source: https://github.com/flutter/flutter/issues/11321#

Betty answered 8/11, 2020 at 14:52 Comment(1)
Thank Ercross for this answer. You just save from suicide man!!!Camarena
M
-1

mediaQuery to get height, was inside build. It works, but here it caused error.

Code Before:- Widget build(BuildContext context) { double _boxHeight = MediaQuery.of(context).size.height * 0.2; ....

Code After:- Widget build(BuildContext context) { double _boxHeight = 20; ....

Modestomodesty answered 6/10, 2021 at 22:19 Comment(2)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewStenography
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Graminivorous

© 2022 - 2024 — McMap. All rights reserved.