LateInitializationerror:field' searchResults'尚未初始化

发布于 2025-02-12 18:34:59 字数 948 浏览 0 评论 0原文

我尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2025-02-19 18:34:59

尽管searchResults将从未来获取数据,但最好将FutureBuilder用于NectelessWidget或Nullabe数据。

现在,在州课上,

  List<PlaceSearch>? searchResults;

  @override
  void initState() {
    super.initState();
    searchPlaces("yourSearchTerm")
  }

 searchPlaces(String searchTerm) async {
    searchResults = await placeService.getAutoComplete(searchTerm);
    setState((){});
  }

您可以检查列表是否为null。

child: searchResults==null? Text("loading") :ListView.builder(...)

While searchResults will get data from future it is better to use FutureBuilder for statelesswidget or nullabe data.

On state class

  List<PlaceSearch>? searchResults;

  @override
  void initState() {
    super.initState();
    searchPlaces("yourSearchTerm")
  }

 searchPlaces(String searchTerm) async {
    searchResults = await placeService.getAutoComplete(searchTerm);
    setState((){});
  }

Now you can check whether list is null or not.

child: searchResults==null? Text("loading") :ListView.builder(...)
二货你真萌 2025-02-19 18:34:59

您没有在init函数中调用搜索范围函数,因此它不能初始化searchResults变量。为了解决此操作:

@override
initState(){
  super.initState();
  searchPlaces();  
}

并记住在搜索场所功能中设置状态。

You are not calling the searchPlaces function in the init function so it can't initialize the searchResults variable. To solve it do this:

@override
initState(){
  super.initState();
  searchPlaces();  
}

and remember to set state in searchPlaces function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文