send data through arguments with GetX
Asked Answered
S

3

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),),
Saez answered 15/9, 2021 at 5:35 Comment(0)
G
13

You can also used parameters.

 var  data = {
      "email" : "[email protected]",
      "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
Garvy answered 15/9, 2021 at 6:0 Comment(4)
thank you, but my question was is it a best way to do that? or maybe send it by constructor? or another way?Saez
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.Garvy
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.Hideandseek
you mean like 1 view have different controllers and connected thru binding and can go everywhere else on which routes?Garvy
I
6

You can simply use arguments

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

on the second screen, you can do

final title = Get.arguments as String;

Ingaborg answered 15/9, 2021 at 8:30 Comment(0)
C
2

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.

Curst answered 30/1, 2023 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.