Flutter FutureBuilder 不等待 App 更新

Flutter FutureBuilder Does Not Wait When App Updates

问题 我的 FutureBuilder 在应用首次运行时等待,但在应用更新时不等待。

当我的应用完成加载并且我更改为不同的 ToggleButton 时,FutureBuilder 立即开始重新运行,而不是等待 getData() 并在 getData() 完成之前完全完成,然后当 getData() 最终完成时,FutureBuilder 再次运行。

应用程序首次运行时不会出现此问题。当应用首次运行时,FutureBuilder 等待 getData() 在 运行.

之前完成

我需要 FutureBuilder 等待 getData() 在按下不同的按钮时完成,就像应用程序首次按下时一样启动。

注意:为了提高可读性,我删除了尽可能多的不必要代码。如果有帮助,我可以添加更多代码。

代码:

class PriceScreenState extends State<PriceScreen> {
  String selectedCurrency = 'USD';
  String selectedGraphType = "1D";
      var isSelectedGraph = <bool>[true, false, false, false, false, false];

getData() async {
    isWaiting = true;
    try {
      Map graphData = await GraphData().getGraphData(
          selectedCurrency: selectedCurrency,
          selectedGraphType: selectedGraphType);
      isWaiting = false;
      setState(() {
        graphValues = graphData;
      });
    } catch (e) {
      print(e);
    }
  }

 @override
  void initState() {
    super.initState();
    futureData = getData();
  }

@override
  Widget build(BuildContext context) {
...(other code)...
ToggleButtons( ****************TOGGLEBUTTONS***********
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 16.0),
                    child: Text('1D'),
                  ),
                 ...(more Buttons)...
                ],
                onPressed: (int index) {
                  setState(() {
                    for (int buttonIndex = 0;
                        buttonIndex < isSelectedGraph.length;
                        buttonIndex++) {
                      if (buttonIndex == index) {
                        isSelectedGraph[buttonIndex] = true;
                        selectedGraphType = graphType[buttonIndex];
                      } else {
                        isSelectedGraph[buttonIndex] = false;
                      }
                    }
                  });
                  getData();
                },
                isSelected: isSelectedGraph,
              ),
              Expanded(
                child: FutureBuilder( *************FUTUREBUILDER*********
                    future: futureData,
                    builder: (context, snapshot) {
                      if (graphValues.isEmpty) {
                        return new Container();
                      } else {
                        return Graph(graphValues);
                      }
                    }),
              )

因为您正在使用 FutureBuilder,所以您不需要再调用 setState。这是您的代码的可能返工:

Future<Map> futureData;

Future<Map> getData() async {
   try {
      Map graphData = await GraphData().getGraphData(
         selectedCurrency: selectedCurrency,
         selectedGraphType: selectedGraphType,
      );
      return graphData;
   } catch (e) {
      throw Exception(e);
   }
}

@override
void initState() {
   super.initState();
   futureData = getData();
}

@override
Widget build(BuildContext context) {
   // Only coding the FutureBuilder for the example
   return FutureBuilder<Map>(
      future: futureData,
      builder: (context, snapshot) {
         // Future is still loading
         if (!snapshot.hasData)
            return CircularProgressIndicator();
         else if (snapshot.data.isEmpty)
            return Container();
         else
            return Graph(snapshot.data);
      },
   );
}

要使 FutureBuilder 正常工作,您需要 return 在 getData 中设置一个值并使用 snapshot 变量。