What is the best form to pass data between screen with GetX?

Viewed 2369

I am new on Flutter, and I am using GetX.

I want to do a Stepper for registering, and I want to pass one object between 4 screens and fill some data on each screen. What is the best method to do that? I was thinking to put a UserModel on a controller and pass it between screens.

Thank you so much.

2 Answers

Generally you don't need to manually pass around data to different pages when using most state management solutions. You store the relevant data in a single instance of the object that you access from anywhere.

class StepController extends GetxController {
  // any data in this class is accessible from anywhere and doesn't 
  // need to be manually passed to any pages
}

Use the variables you create in the GetX class for the Steps and then from anywhere in the app you find the controller with

final controller = Get.find<StepController>();

Just make sure you initialize once it at some point with

Get.put(StepController());

I haven't use GetX but MobX(+provider). In your case, I think dependency injection is best way to share store between screens, you can consume the stores everywhere, so you dont need to pass data to other screen.

Related