使用 GetX 通过参数发送数据

Osa*_*med 5 performance arguments flutter flutter-getx

使用 GetX (或任何其他方式)在 flutter 中发送带有参数的数据是一个好习惯吗?我的意思是它对性能和内存容量有好处吗?...就像这个例子:

Get.toNamed(AppPages.servicesDetails, arguments: [service]);
Run Code Online (Sandbox Code Playgroud)

当(服务)仅包含来自 API 的一种产品的数据时:例如(id、名称、信息、图像...等)。

并在服务详细信息页面中:

 final s = Get.arguments[0];
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      body:  Center(child: Text(s.name),),
Run Code Online (Sandbox Code Playgroud)

Arb*_*hil 12

您还可以使用参数。

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

从其他页面获取也是这样的。

  print(Get.parameters['email']);
Run Code Online (Sandbox Code Playgroud)

另外,在 Getx 上,您可以像文档所写的数据上的 url 链接一样传递它。

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

如果你想传递整个项目数据,如果有 onTap 函数,你也可以从列表中传递模型,尽管你需要再次解码它

例如

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]),
    }
 ),
),
Run Code Online (Sandbox Code Playgroud)

来自控制器

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;
   
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,从 ui 调用数据将是这样的

final controller = Get.put(MyXampleController());
///// this will be the data we got from the item
controller.model.value.name
Run Code Online (Sandbox Code Playgroud)


小智 6

您可以简单地使用参数

Get.toNamed(
      '/my-route',
      arguments: "Hello",
    );
Run Code Online (Sandbox Code Playgroud)

在第二个屏幕上,您可以执行以下操作

final title = Get.arguments as String;

Run Code Online (Sandbox Code Playgroud)