How can we pass List and variable through named route in Getx Flutter?

146 Views Asked by At

I pass data in getx routes but I don't understand how to get data on the next page.

List<UserModel> list = [];

var itemid = snapshot.data?.docs[index].id;


     Get.toNamed(RoutesName.editAddNewHome_Screen,
                                arguments: [List[index],itemid ]);

I tried to get data on the next page but it showed an error.

List<dynamic> args = Get.arguments;

 List<String> listData = args[0];
   var myVariable = args[1];

error - The instance member 'args' can't be accessed in an initializer.

1

There are 1 best solutions below

1
On

With Getx Routing data are passed as map to an arguments

Get.toNamed(RoutesName.editAddNewHome_Screen,arguments: {"itemId: itemid ]);

In your case for retrieving the passed data using Get.arguments["itemId"], You can retrieve this data directly on your Widget, or get the value on initState this will solve error - The instance member 'args' can't be accessed in an initializer.

String itemId = “”;

@override
void initState() {
  itemId= Get.arguments[“itemId”]??"”
  super.initState();
}

But I would suggest making this data required on the screen and then passing this data on your route file

In your route file:

 GetPage(name: RoutesName.editAddNewHome_Screen, page: () => EditAddNewHome_Screen(itemId: Get.arguments["itemId"] as String), ),

For in-depth understanding, check: https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md