当应用程序更新时,Flutter FutureBuilder不等待

时间:2020-10-28 12:19:20

标签: flutter flutter-futurebuilder

问题。我的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);
                      }
                    }),
              )

1 个答案:

答案 0 :(得分:1)

在使用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正常工作,您需要在getData中返回一个值并使用snapshot变量。