如何在Flutter中将应用程序屏幕上的数据保留为原样?

4

Here is a video of an issue that I am facing, every time I come on the dashboard It calls an API to fetch that image, is there any way that I can keep this image here permanently because this image is going to change on daily basis?

我遇到了一个问题,有一个屏幕我在调用API获取数据,但是当我去其他屏幕工作时,再回到主屏幕(即调用API的屏幕)时,它又会重新调用API。我使用加载器等待API获取数据,但每次都调用API让人感到沮丧。
在Flutter中有什么功能或方法可以让我在关闭应用之前保持已获取的数据不变吗?
这是我遇到的问题的视频,每次进入仪表板时都会调用API来获取该图像,是否有任何方法可以永久地保留此图像,因为该图像将每天更改?

Please include your sources - Jungwon
是的,我现在已经上传了。 - Tejas Gaikwad
您可以在此处查看类似问题的解决方案,使用AutomaticKeepAliveClientMixin: https://dev59.com/Sazka4cB1Zd3GeqP7FlZ - ayinloya
1个回答

2

在 API 调用后,您可以使用 shared_preferences 将数据保存到本地。然后,每当您返回到该屏幕时,都可以使用这些数据。

 Future addSharePref() async{

SharedPreferences prefs = await SharedPreferences.getInstance();

await prefs.setString('storingName', yourVarName);
}

然后,当您再次打开屏幕时,您可以从 shared_preferences 中调用这些值。类似于这样:

yourVarName= preferences.getString('storingName')?? "No name found";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接