stacked icon indicating copy to clipboard operation
stacked copied to clipboard

Pass arguments directly to a ViewModel when navigating

Open FilledStacks opened this issue 6 years ago • 4 comments

I've seen the question come up a few times around how to pass arguments to a ViewModel. Currently this is done by passing it to the View and then passing it into the viewModelBuilder that constructs the ViewModel.

This issue serves as a reminder to explore the syntax and code maintenance improvements that would bring, if any and then make a decision to see if it's worth adding this functionality.

FilledStacks avatar Aug 18 '20 12:08 FilledStacks

what if you want to call a future with the argument data? i use an on model ready function is their a better way?

ajnaf avatar Jan 24 '21 20:01 ajnaf

i usually done this

class ProfileViewModel extends FutureViewModel<void>{
final int id;

final _http = locator<HttpService>();

ProfileViewModel({this.id});

Future<void> futureToRun(){
       return _http.get('/users/$id');
    }
}

class ProfileView extends ViewModelBuilderWidget<ProfileViewModel>{

final int id;

ProfileView({this.id});

@override
ProfileViewModel viewModelBuilder(){
      return ProfileViewModel(id: id);
   }
}

alvinvin00 avatar Jan 25 '21 01:01 alvinvin00

@ajnaf that's perfectly fine. You can also pass your data into the constructor and pass it to the futureToRun as it's shown above.

FilledStacks avatar Jan 25 '21 05:01 FilledStacks

This feature is not something we'll spend time on now. I think it's a good idea but the restrictions it'll add to the code is not something i'd like to tackle right now.

FilledStacks avatar Apr 02 '21 15:04 FilledStacks