简体   繁体   English

Flutter riverpod 提供程序未在 UI 中更新 state

[英]Flutter riverpod provider not updating state in UI

I'm right now connecting my flutter mobile app to the backend API, and i am using providers from Riverpod packagee, as a way to reload the widget when there is data changes.我现在正在将我的 flutter 移动应用程序连接到后端 API,并且我正在使用来自 Riverpod packagee 的提供程序,作为在数据发生变化时重新加载小部件的一种方式。

The provider that i am using is ChangeNotifierProvider.我使用的提供商是 ChangeNotifierProvider。 And i notice one thing, i am creating now the user account, where they can view their profile and edit it.我注意到一件事,我现在正在创建用户帐户,他们可以在其中查看和编辑他们的个人资料。 While calling the api the values will be null, but when the provider gets the data shoudl reload the widget and show the data in the form fields with the data that came from the backend.在调用 api 时,值将为 null,但是当提供者获取数据时,应该重新加载小部件并使用来自后端的数据在表单字段中显示数据。

This is how my provider is working, it gets a parameter of type String, that will be the user id and then it will call the backend to fetch the data from that user.这就是我的提供者的工作方式,它获取一个 String 类型的参数,这将是用户 ID,然后它将调用后端以从该用户获取数据。

在此处输入图像描述

After that i have the class UserProxy, that will do the backend call, and on the user screen i have a listener, like this之后我有 class UserProxy,它将进行后端调用,在用户屏幕上我有一个监听器,就像这样在此处输入图像描述

After that in the form initial data i have called the user data that comes from the backend to show as initial data之后在表单初始数据中我调用了来自后端的用户数据以显示为初始数据在此处输入图像描述

But the problem here is the thing, as you can see in the console i have a print, where i print the username to check if any data passes, and it does, but the UI doesnt update as you can see但这里的问题是,正如您在控制台中看到的那样,我打印了用户名以检查是否有任何数据通过,确实如此,但是 UI 没有像您看到的那样更新在此处输入图像描述

if you see it comes first null, obviously, sisnce the backend is still beeing called but then it gets the username, but the UI doesnt update...如果你看到它首先出现 null,显然,因为后端仍在被调用,但随后它获得了用户名,但 UI 没有更新......

Any help will be amazing, thanks in advance任何帮助都会很棒,在此先感谢

Well i figure it out, the problem was that i was using a initial value in the TextFormFiled, instead of using the controller, because that is the one who checks for changes, here is how my code works now, just needed to change the form field:好吧,我想通了,问题是我在 TextFormFiled 中使用了一个初始值,而不是使用 controller,因为这是检查更改的那个,这是我的代码现在的工作方式,只需要更改表单场地: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM