Flutter View-ViewModel with Provider

116 views
Skip to first unread message

Black Clover

unread,
Dec 18, 2019, 2:10:35 AM12/18/19
to Flutter Development (flutter-dev)
I'm refactoring my Flutter code.
By planning to separate the View and State Management in separate files.

1. Am I doing it right?
I created a ViewModel class that inherits the ChangeNotifier class as follows.
2. my problem now is, every time I open the page. The state still stores the past state. Is there a way to restore the state to the beginning? For example the value of the message variable in the example above. Do I need to set it manually, by making the onInit () function in the ViewModel class.

haijun gu

unread,
Dec 19, 2019, 5:37:14 AM12/19/19
to Flutter Development (flutter-dev)
Try elm architecture(TEA)

在 2019年12月18日星期三 UTC+8下午3:10:35,Black Clover写道:

Madhukesh D

unread,
Dec 20, 2019, 9:53:05 PM12/20/19
to Flutter Development (flutter-dev)
https://marketplace.visualstudio.com/items?itemName=madhukesh040011.flutter-mvvm-architecture-generator&ssr=false

Check this flutter extension..this will help you initialze and create views easily with views and viewmodel

Chagnas Emmanuel

unread,
Dec 26, 2019, 1:06:01 AM12/26/19
to Flutter Development (flutter-dev)
Hi,
I think that changing your architecture can not resolved your problem, because your problem is "why my code doesn't work", right? :)

If I understand, your viewmodel is in a provider, so the value is saved until you kill the application, but you try to "reset" the viewmodel with the dispose?

Have you try to reset the value of "message" in the dispose (by adding a method "reset" in the viewmodel)?

I think the viewmodel is still in the provider when you close the page, even with the dispose...

Regards,
Reply all
Reply to author
Forward
0 new messages