Flutter:SetState() 更新值但不在屏幕上呈现。只有在 Flushbar 中使用更新值时才会发生这种情况
Flutter : SetState() updates values but does not render on the screen. This happens only when using the updated values inside a Flushbar
我在 Flushbar 中有一个文本字段,该文本字段根据提供的输入使用 Google 地点 API 来搜索地点。我在文本字段的 'onChanged' 参数上调用一个函数,如代码中所示,但这不会显示在屏幕上,因为在文本字段中键入了一些内容。尽管打印值显示值已更新。此外,在弹出 Flushbar 并将其重新打开时会显示预期结果。
此外,只有在 Flushbar 中使用更新值时才会发生这种情况。它在脚手架上按预期呈现。
我知道这与之前提出的其他问题类似,但似乎没有提供对我有用的答案。我什至发现这可能会发生,因为 Flushbar 本身没有 'state',因此 'setState()' 不会影响其内容。但我不知道如何解决这个问题。对此的任何帮助将不胜感激。
更新:我尝试在 Flushbar 中使用 StatefulBuilder 小部件,但它给出了类似的结果。我在 Flushbar.ListView.builder 之后的 return 语句中调用了 StaefulBuilder。
示例代码:
@override
Widget build (BuildContext context){
return Scaffold(
body : FlatButton(
child : Text('Search Location'),
onPressed : Flushbar(
flushbarPosition: FlushbarPosition.BOTTOM,
userInputForm : Form(
child : Container(
child : Column(
children : <Widget>[
getSearchFieldFlushBar(),
_displaySearchedPlaces.length != 0
? ListView.builder(
shrinkWrap : true,
itemCount : _displaySearchedPlaces.length
itemBuilder : (BuildContext context, index){
return ListTile(
title : Text(_displaySearchedPlaces[index])
);
})
: Container(height : 0, width : 0),
])
)
)
)..show(context);
));
}
这是调用文本字段的函数:
TextFormField getSearchFieldFlushbar(){
return TextFormField(
style: TextStyle(color: Colors.black),
onChanged: (text) {
getLocationResults(text);
},
);
这是在文本字段中输入内容时调用的函数:
void getLocationResults(String input) async {
String baseURL = 'https://maps.googleapis.com/maps/api/place/autocomplete/json';
String type = 'geocode';
String request = '$baseURL?input=$input&key=$places_Api_Key&type=$type';
Response response = await Dio().get(request);
final predictions = response.data['predictions'];
_displayResults = [];
for (int i = 0; i < predictions.length; i++) {
String name = predictions[i]['structured_formatting']['main_text'];
_displayResults.add(name);
}
setState(() {
_displaySearchedPlaces = _displayResults;
});
print(_searchedPlacesResults);
}
}
Screenshot1
ScreenShot2
我终于设法使用之前在 Whosebug 上对类似问题给出的一些答案解决了这个问题,例如 and here。
基本上,我们使用 StatefulBuilder Widget 并将所有可能会更新的子 widget 放入此 widget 中。正如问题中提到的,最初使用它对我不起作用,但是,它在一段时间后开始起作用。
作为参考,我在表单小部件的 'child' 参数处添加了 StatfulBuilder 小部件。
我在 Flushbar 中有一个文本字段,该文本字段根据提供的输入使用 Google 地点 API 来搜索地点。我在文本字段的 'onChanged' 参数上调用一个函数,如代码中所示,但这不会显示在屏幕上,因为在文本字段中键入了一些内容。尽管打印值显示值已更新。此外,在弹出 Flushbar 并将其重新打开时会显示预期结果。
此外,只有在 Flushbar 中使用更新值时才会发生这种情况。它在脚手架上按预期呈现。
我知道这与之前提出的其他问题类似,但似乎没有提供对我有用的答案。我什至发现这可能会发生,因为 Flushbar 本身没有 'state',因此 'setState()' 不会影响其内容。但我不知道如何解决这个问题。对此的任何帮助将不胜感激。
更新:我尝试在 Flushbar 中使用 StatefulBuilder 小部件,但它给出了类似的结果。我在 Flushbar.ListView.builder 之后的 return 语句中调用了 StaefulBuilder。
示例代码:
@override
Widget build (BuildContext context){
return Scaffold(
body : FlatButton(
child : Text('Search Location'),
onPressed : Flushbar(
flushbarPosition: FlushbarPosition.BOTTOM,
userInputForm : Form(
child : Container(
child : Column(
children : <Widget>[
getSearchFieldFlushBar(),
_displaySearchedPlaces.length != 0
? ListView.builder(
shrinkWrap : true,
itemCount : _displaySearchedPlaces.length
itemBuilder : (BuildContext context, index){
return ListTile(
title : Text(_displaySearchedPlaces[index])
);
})
: Container(height : 0, width : 0),
])
)
)
)..show(context);
));
}
这是调用文本字段的函数:
TextFormField getSearchFieldFlushbar(){
return TextFormField(
style: TextStyle(color: Colors.black),
onChanged: (text) {
getLocationResults(text);
},
);
这是在文本字段中输入内容时调用的函数:
void getLocationResults(String input) async {
String baseURL = 'https://maps.googleapis.com/maps/api/place/autocomplete/json';
String type = 'geocode';
String request = '$baseURL?input=$input&key=$places_Api_Key&type=$type';
Response response = await Dio().get(request);
final predictions = response.data['predictions'];
_displayResults = [];
for (int i = 0; i < predictions.length; i++) {
String name = predictions[i]['structured_formatting']['main_text'];
_displayResults.add(name);
}
setState(() {
_displaySearchedPlaces = _displayResults;
});
print(_searchedPlacesResults);
}
}
Screenshot1 ScreenShot2
我终于设法使用之前在 Whosebug 上对类似问题给出的一些答案解决了这个问题,例如
基本上,我们使用 StatefulBuilder Widget 并将所有可能会更新的子 widget 放入此 widget 中。正如问题中提到的,最初使用它对我不起作用,但是,它在一段时间后开始起作用。
作为参考,我在表单小部件的 'child' 参数处添加了 StatfulBuilder 小部件。