Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: getTextBeforeCursor on inactive InputConnection

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

like image 656
loma Avatar asked Jul 22 '20 20:07

loma


3 Answers

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

like image 125
anas Avatar answered Nov 17 '22 07:11

anas


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

like image 45
happy_san Avatar answered Nov 17 '22 06:11

happy_san


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.

like image 1
Pedro R. Avatar answered Nov 17 '22 08:11

Pedro R.