5

is it a good practice to send data with arguments in flutter using GetX (or any other way)? i mean is it good for performance and ram capcity ? ... like this example:

Get.toNamed(AppPages.servicesDetails, arguments: [service]);

when (service) contain a data for only one product came from API : like (id, name, info, image ...etc).

and in the servicesDetails page:

 final s = Get.arguments[0];
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      body:  Center(child: Text(s.name),),
Osama Mohammed
  • 2,433
  • 13
  • 29
  • 61

3 Answers3

9

You can also used parameters.

 var  data = {
      "email" : "test@gmail.com",
      "message" : "hi!"
   };
  Get.toNamed(YourRouteName.name, parameters: data);

Also from getting it from another pages is like this.

  print(Get.parameters['email']);

Also on Getx you can pass it like a url link on data as the document written.

https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md

If you want to pass whole item data, You can also pass a model from list if have onTap function though you need to decode it again

e.g

MyCardItemFromList(
 name: list[index].name,
 ontapFunction: () => Get.toNamed(
   YourRouuteName.name,
   parameters: {
       /// Lets assume this is the item selected also it's own item data
       "itembyIndex": jsonEncode(list[index]),
    }
 ),
),

from controller

class MyXampleController extends GetxController{

//declare the model
final Rx<Model> model = Model().obs;


 @override
  void onInit() {
    convertToDecode();
    super.onInit();
  } 
  convertToDecode(){
    final decode = jsonDecode(Get.parameters['itembyIndex']) 
    final passToModel = Model.fromJson(decode);
    model(passToModel);
    // or if you are using getBuilder
    // try do it like this
    // model.value = passToModel;
     // update();
    // don't forget to call update() since it's needed from getbuilder;
   
  }
}

Now, For calling the data from ui will be like this

final controller = Get.put(MyXampleController());
///// this will be the data we got from the item
controller.model.value.name
Arbiter Chil
  • 1,061
  • 1
  • 6
  • 9
  • thank you, but my question was is it a best way to do that? or maybe send it by constructor? or another way? – Osama Mohammed Sep 15 '21 at 13:08
  • It's a yes for passing array but what if the way you pass your item or data is more quite complex like json and you might lost which data is it pass so sometimes using Get.arguments[0] might be difficult identifying which data was pass so basically parameters is a good choice since you will called it by Get.parameters['email'] so that you will know which data you pass and used it from. While if you use constructor to pass data to another page is also good but what if data you passing is one by one ex. name,address, and so forth like an information that is long so it will quite a long pass. – Arbiter Chil Sep 15 '21 at 14:04
  • I have passed json as an argument. Can I use GetX for one or two routes but Go Router everywhere else? It seems that after using getx I get go router errors. – Satchel Jul 19 '22 at 16:28
  • you mean like 1 view have different controllers and connected thru binding and can go everywhere else on which routes? – Arbiter Chil Jul 19 '22 at 16:35
5

You can simply use arguments

Get.toNamed(
      '/my-route',
      arguments: "Hello",
    );

on the second screen, you can do

final title = Get.arguments as String;

aman
  • 205
  • 2
  • 4
1

There is another way of accessing values on another page

By accessing the Controller

ControllerName obj = Get.find();

obj.function();

obj.variable;

obj.anythingYouWantToAccess;

& use it like it's on the current Controller

NOTE: The Controller you want to access should b open in simple words it should be the previous screen.

hayatbangash55
  • 127
  • 1
  • 8