且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于Flutter中提供者的说明-如何保存数据

更新时间:2023-01-19 08:57:57

提供者是一个非常好的工具,它可以更新小部件树,而无需调用它在树部件中起作用的setstate方法.
ChangeNotifier类:

providers are a really good tool to update your widget tree without calling the setstate method it work in tree part.
the ChangeNotifier class :

在此类中,您可以处理应用程序的任何逻辑,例如发送请求来管理数据响应.

in this class you can handle any logic of your app,in your case is sending request managing the data response.

ChangeNotifier构造函数provider widget:

The ChangeNotifier constructor provider widget:

在这里您将初始化更改通知程序类,该类应该在窗口小部件树中的较高位置,以便在完成某些逻辑后通知依赖于逻辑代码(更改通知程序)的子项.

this is where you will initialize your change notifier class it should be upper in your widget tree, so child which depends of your logic code (Change notifier) get notified when some logic is done.

如何收听更改Consumer widget and provider.of<ChangeNotifierClass>(context):

How to listen to changes Consumer widget and provider.of<ChangeNotifierClass>(context):

这些小部件在您的更改通知程序类中侦听notifyListeners()调用,因此每次逻辑运行正在侦听您的ChangeNotifier的所有子级小部件时,build方法都将运行(因此其基本方法类似于调用setstate).

these widget listen to notifyListeners() call inside your change notifier class so each time logic runs all the child widget which are listening to your ChangeNotifier the build method will run (so its basicly like calling setstate).

因此,对于您的问题,您只需要实现围绕请求的逻辑,即可根据json数据和内容来构建列表....而不是运行notifylisteners来显示您对该数据感兴趣的小部件和其新状态.
对我来说,很难接受这样的逻辑,但是经过一段时间后,我清楚地看到了它与颤动非常吻合,要获得更多帮助,您可以检查整体模式的体系结构.
希望它对您有帮助.

So for your question you just need to implement the logic around your request building your lists from json data and stuff... . than running notifylisteners to display and new state of your widgets which are interest in that data .
For me it was pretty hard to accept such logic but after done that one time i clearly saw that fit pretty well with flutter , and for more help you can check the bloc-pattern architecture .
hope it helped you good luck .