Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 未来<;列表<;动态>&燃气轮机;无法分配给列表<;动态>;_Flutter_Dart - Fatal编程技术网

Flutter 未来<;列表<;动态>&燃气轮机;无法分配给列表<;动态>;

Flutter 未来<;列表<;动态>&燃气轮机;无法分配给列表<;动态>;,flutter,dart,Flutter,Dart,当我尝试将此方法的返回分配给小部件参数时,获取错误(标题)。建议:是一个列表而不是未来。FutureBuilder是唯一的方法吗?此小部件中的AutoCompleteTextField是一个前置类型,因此将在击键停止后每0.5秒调用一次GetLocationSuggestionList(不确定这在回答此问题时是否重要) @覆盖 小部件构建(构建上下文){ 返回容器( 孩子:新中心( 子项:列(子项:[ 新栏目(儿童:[ searchTextField=AutoCompleteTextField(

当我尝试将此方法的返回分配给小部件参数时,获取错误(标题)。
建议:
是一个
列表
而不是
未来
。FutureBuilder是唯一的方法吗?此小部件中的
AutoCompleteTextField
是一个前置类型,因此将在击键停止后每0.5秒调用一次GetLocationSuggestionList(不确定这在回答此问题时是否重要)

@覆盖
小部件构建(构建上下文){
返回容器(
孩子:新中心(
子项:列(子项:[
新栏目(儿童:[
searchTextField=AutoCompleteTextField(
建议:GetLocationSuggestionList(“sd”),
样式:新文本样式(颜色:Colors.black,fontSize:16.0),
装饰:新的输入装饰(
.....
未来GetLocationSuggestionList(字符串位置文本)异步{
列表建议列表=列表();
映射建议KeyValuePairs=Map();
动态数据=等待GoogleMapsServices.getAddressPrediction(
位置文本,
LatLng(当前位置.纬度,当前位置.经度),
);
如果(数据!=null){
for(数据中的动态预测。预测){
SuggestionKeyValuePairs[predictions.description]=predictions.placeId;
如果(!suggestionList.contains(predictions.description))
suggestionList.add(预测.描述);
}
返回建议列表;
}否则{
返回[''];
}
}

此错误的原因是suggestions参数预期的是列表,而不是未来

您可以创建一个状态变量,并使用
setState()
调用或任何其他状态管理机制将
getLocationSuggestionList()
函数的结果分配给该变量,以便在状态发生更改时,使用相关数据再次构建UI

class YourClass extends StatefulWidget {
 ///
}

class _YourClassState extends State<YourClass>{

   /// Your state variable here. Initialize with data that will be showing if actual data not available.
   List<dynamic> suggestionList = ["];


   initState(){
     /// call you get suggestion function on init or any other lifecycle methods as per your need, may be inside build
     getLocationSuggestionsList("sd");
     super.initState();
   }


   @override
   Widget build(context){
       return AutoCompleteTextField<dynamic>(
            suggestions: suggestionList,
            style: new TextStyle(color: Colors.black, fontSize: 16.0),
            decoration: new InputDecoration()
            /// ....
       );

   }



   void getLocationSuggestionsList(String locationText) async {
    List<String> sList = List();
    Map suggestionsKeyValuePairs = Map<String, String>();
    dynamic data = await GoogleMapsServices.getAddressPrediction(
      locationText,
      LatLng(currentLocation.latitude, currentLocation.longitude),
    );
    if (data != null) {
      for (dynamic predictions in data.predictions) {
        suggestionsKeyValuePairs[predictions.description] = predictions.placeId;
        if (!sList.contains(predictions.description))
          sList.add(predictions.description);
      }
    } else {
     sList =  [''];
    }

    setState((){
      suggestionList = List;
      /// This will render your UI again with updates suggestionList
    });
  }
}

class YourClass扩展StatefulWidget{
///
}
类YourClassState扩展了状态{
///此处是您的状态变量。使用实际数据不可用时将显示的数据初始化。
列表建议列表=[“];
initState(){
///调用init上的get suggestion函数,或者根据需要调用任何其他生命周期方法,这些方法可能在构建内部
GetLocationSuggestionList(“sd”);
super.initState();
}
@凌驾
小部件构建(上下文){
返回AutoCompleteTextField(
建议:建议列表,
样式:新文本样式(颜色:Colors.black,fontSize:16.0),
装饰:新输入装饰()
/// ....
);
}
void GetLocationSuggestionList(字符串位置文本)异步{
List sList=List();
映射建议KeyValuePairs=Map();
动态数据=等待GoogleMapsServices.getAddressPrediction(
位置文本,
LatLng(当前位置.纬度,当前位置.经度),
);
如果(数据!=null){
for(数据中的动态预测。预测){
SuggestionKeyValuePairs[predictions.description]=predictions.placeId;
如果(!sList.contains(predictions.description))
添加(预测、描述);
}
}否则{
sList=[''];
}
设置状态(){
suggestionList=列表;
///这将使用更新建议列表再次呈现您的UI
});
}
}

此错误的原因是suggestions参数预期的是列表,而不是未来

您可以创建一个状态变量,并使用
setState()
调用或任何其他状态管理机制将
getLocationSuggestionList()
函数的结果分配给该变量,以便在状态发生更改时,使用相关数据再次构建UI

class YourClass extends StatefulWidget {
 ///
}

class _YourClassState extends State<YourClass>{

   /// Your state variable here. Initialize with data that will be showing if actual data not available.
   List<dynamic> suggestionList = ["];


   initState(){
     /// call you get suggestion function on init or any other lifecycle methods as per your need, may be inside build
     getLocationSuggestionsList("sd");
     super.initState();
   }


   @override
   Widget build(context){
       return AutoCompleteTextField<dynamic>(
            suggestions: suggestionList,
            style: new TextStyle(color: Colors.black, fontSize: 16.0),
            decoration: new InputDecoration()
            /// ....
       );

   }



   void getLocationSuggestionsList(String locationText) async {
    List<String> sList = List();
    Map suggestionsKeyValuePairs = Map<String, String>();
    dynamic data = await GoogleMapsServices.getAddressPrediction(
      locationText,
      LatLng(currentLocation.latitude, currentLocation.longitude),
    );
    if (data != null) {
      for (dynamic predictions in data.predictions) {
        suggestionsKeyValuePairs[predictions.description] = predictions.placeId;
        if (!sList.contains(predictions.description))
          sList.add(predictions.description);
      }
    } else {
     sList =  [''];
    }

    setState((){
      suggestionList = List;
      /// This will render your UI again with updates suggestionList
    });
  }
}

class YourClass扩展StatefulWidget{
///
}
类YourClassState扩展了状态{
///此处是您的状态变量。使用实际数据不可用时将显示的数据初始化。
列表建议列表=[“];
initState(){
///调用init上的get suggestion函数,或者根据需要调用任何其他生命周期方法,这些方法可能在构建内部
GetLocationSuggestionList(“sd”);
super.initState();
}
@凌驾
小部件构建(上下文){
返回AutoCompleteTextField(
建议:建议列表,
样式:新文本样式(颜色:Colors.black,fontSize:16.0),
装饰:新输入装饰()
/// ....
);
}
void GetLocationSuggestionList(字符串位置文本)异步{
List sList=List();
映射建议KeyValuePairs=Map();
动态数据=等待GoogleMapsServices.getAddressPrediction(
位置文本,
LatLng(当前位置.纬度,当前位置.经度),
);
如果(数据!=null){
for(数据中的动态预测。预测){
SuggestionKeyValuePairs[predictions.description]=predictions.placeId;
如果(!sList.contains(predictions.description))
添加(预测、描述);
}
}否则{
sList=[''];
}
设置状态(){
suggestionList=列表;
///这将使用更新建议列表再次呈现您的UI
});
}
}

getLocationSuggestionList()
是异步的并返回一个未来,如果您想得到结果(
List
),需要使用
wait
关键字调用它

await getLocationSuggestionsList("sd")
但是,这只适用于异步函数/方法

您可以通过多种方式解决此问题:

  • 使用FutureBuilder
  • 采用反应式编程架构(Bloc、Rx、原始流等)
  • 像奎师那库马肯那样做;)

getLocationSuggestionList()
是异步的并返回一个未来,如果您想得到结果(
List
),需要使用
wait
关键字调用它

await getLocationSuggestionsList("sd")
但是,这只适用于异步函数/方法

您可以通过多种方式解决此问题:

  • 使用FutureBuilder
  • D