簡體   English   中英

如何在 flutter 中保存上一頁導航的 state

[英]How to save state of previous page on navigation in flutter

I am building a flutter application in which I have 2 screens.I have API calls on both screens.When I navigate on 2nd screen and return back to 1st screen then build function of 1st Screen runs as expected and API is called again(In future FutureBuilder),我不想要。 當我在屏幕上從上到下滑動時,我只想更新或獲取 API。但是由於 my.st 屏幕構建 function 中的 FutureBuilder,它每次都一次又一次地獲取數據。 任何帶有例如代碼的建議都會有所幫助。謝謝!

您可以使用StatefullWidget並在State class 中擁有一個屬性,例如Future<String>並在initState中分配它。
如果您從第二個屏幕返回您的構建 function 調用但您有以前的數據。

...
Future<String> future;

initState(){
    future = apiCall();
}

Widget build(){
   return FutureBuilder(
     future: future,
   );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM