LateInitializationerror:field' searchResults'尚未初始化
我尝试使用Google Map AutoComplete,但是我发现了此错误lateInitializationError:field'searchResults'尚未初始化。
如何解决IT问题?
我仍然试图使用这种方式,但仍然很疲倦。
void initState() {
searchResults;
}
这是我的代码
late List<PlaceSearch> searchResults;
final placeService = PlaceSerive();
searchPlaces(String searchTerm) async {
searchResults = await placeService.getAutoComplete(searchTerm);
}
...
...
void initState() {
searchResults;
}
...
...
...
child: ListView.builder(
itemCount: searchResults.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(searchResults[index].description,
style: TextStyle(color: Colors.black)),
);
},
),
I tried to used google map autocomplete, but I got the this error LateInitializationError: Field 'searchResults' has not been initialized.
How can solve it problem?
I still tried to used this way, but still falue.
void initState() {
searchResults;
}
This is my code
late List<PlaceSearch> searchResults;
final placeService = PlaceSerive();
searchPlaces(String searchTerm) async {
searchResults = await placeService.getAutoComplete(searchTerm);
}
...
...
void initState() {
searchResults;
}
...
...
...
child: ListView.builder(
itemCount: searchResults.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(searchResults[index].description,
style: TextStyle(color: Colors.black)),
);
},
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管
searchResults
将从未来获取数据,但最好将FutureBuilder用于NectelessWidget或Nullabe数据。现在,在州课上,
您可以检查列表是否为null。
While
searchResults
will get data from future it is better to use FutureBuilder for statelesswidget or nullabe data.On state class
Now you can check whether list is null or not.
您没有在init函数中调用
搜索范围
函数,因此它不能初始化searchResults变量。为了解决此操作:并记住在搜索场所功能中设置状态。
You are not calling the
searchPlaces
function in the init function so it can't initialize the searchResults variable. To solve it do this:and remember to set state in searchPlaces function.