【问题标题】:Rebuild stateful widget on update of other stateful widget in Flutter在 Flutter 中更新其他有状态小部件时重建有状态小部件
【发布时间】:2020-03-18 00:42:16
【问题描述】:

我有两个有状态的小部件。 一个我将调用父级,因为它正在生成一个列表(地图标记),该列表正在传递给第二个小部件(子级,地图,在不同的 dart 文件中)。 我有一个默认的标记列表,它在父小部件的异步函数中更新。 尽管使用 setState() 更新列表,但子小部件(地图)不受此更新的影响。 在将数据传递给构建方法中的小部件之前,我有什么方法可以等待?


class ParentWidget extends StatefulWidget{
        .............
}

class _ParentWidgetState extends State<ParentWidget> {

List markers = [LatLng(0,0)];

@override
  void initState() {
    super.initState();
    updateMarkers();
}

updateMarkers () async {
  updatedMarkers = await getUpdatedMarkers()
  setState((){
    markers = updatedMarkers
  });
  # # print(updatedMarkers)
  # # ^^ prints the correct data that I want to pass in MyMap
}

@override
  Widget build(BuildContext context) {
  return Scaffold(
  ............
  body: MyMap(markers: markers);
 # # The original markers ([LatLng(0,0)]) is the only data getting passed to MyMap
 # # How can I get the updatedMarkers passed to the MyMap widget???????
}

【问题讨论】:

    标签: asynchronous flutter widget


    【解决方案1】:

    我认为这个视频可能是你的答案:https://www.youtube.com/watch?v=d_m5csmrf7I

    他有两个 StatefulWidgets(Slider 和 Chart)。通过使用 Provider,他可以在滑块移动时更改图表数据。

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 2021-06-11
      • 2021-07-25
      • 2019-09-23
      • 2020-10-20
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2020-03-18
      相关资源
      最近更新 更多