9

我正在尝试检索 TextField 标题中的用户输入,以便可以将其传递给名为 void _filterList(value). 但是,每次我输入一些文本时,都会出现以下错误:

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

这是我的代码:

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());
}

这是我的功能:

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

}

据我所知,键盘似乎有问题,但我还没有弄清楚如何防止它

4

5 回答 5

5

确保文本字段的控制器不在构建方法中,并确保小部件是有状态的

于 2021-02-28T00:03:43.193 回答
1

已知问题,在此处跟踪。甚至这个示例代码也会在 android 中给出警告。关于是什么原因没有达成共识。

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

资源

于 2020-08-15T09:24:08.213 回答
1

我在我的项目中也遇到了这个。我的原因是我在MediaQuery.of(context).height返回 Scaffold Widget 的类的 build 方法中调用了代码。我不得不将其删除并将其放在子小部件类中。例如:

在我有...

```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);
     }
}```

我现在有...

```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);
     }
}```

其他一些可行的修复方法是确保您的颤振通道位于稳定通道上。如果没有,可以通过以下命令更改频道flutter channel stable

来源:https ://github.com/flutter/flutter/issues/11321#

于 2020-11-08T14:52:05.203 回答
0

您可以使用 TextEditingController 和 FutureBuilder 来完成。像这样:

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();
      },
    );
          ],
        ),
      ),
    );
  }

注意:我没有测试这段代码,但我写过类似的东西。

于 2020-08-20T22:39:44.083 回答
0

mediaQuery 获取高度,在内部构建。它有效,但在这里它导致了错误。

之前的代码:- Widget build(BuildContext context) { double _boxHeight = MediaQuery.of(context).size.height * 0.2; ……

之后的代码:- Widget build(BuildContext context) { double _boxHeight = 20; ……

于 2021-10-06T22:19:50.060 回答