Home > OS >  how to pass list of maps to another screen using getx in flutter
how to pass list of maps to another screen using getx in flutter

Time:06-25

I am trying to pass a list of maps data(getting from firebase firestore array form) from one screen to another screen using getx in flutter.

This is my code first screen

final cartOrder = orderList.data.docs[index]['cartorder'];
            final orderedDate = orderList.data.docs[index]['ordered_date'];
            final delivered = orderList.data.docs[index]['delivered'];

            return SizedBox(
              height: 100,
              width: double.infinity,
              child: InkWell(
                onTap: (() {
                  Get.toNamed("/order_details?cartOrder=$cartOrder");
                  // Get.to(OrderDetails(),arguments: cartOrder);
                }),
                child: 

This is my second screen

var cartOrder = Get.parameters['cartOrder'];
    @override
    Widget build(BuildContext context) {
      return ListView.builder(
        itemCount: cartOrder.length,
        itemBuilder: (ctx, index) {

when passing data in the first screen the data looks like this (expandable while debugging) length of cartOrder is 2.

cartOrder = 
[
  {'name': 'xyz','id': '123'},
  {'name': 'abc','id': '456'}
]

but when receiving on the second screen not (expandable but looks like concatenating) here length of cartorder is 4(name,id,name,id)

cartOrder = 
[{'name': 'xyz','id': '123'},{'name': 'abc','id': '456'}]

how to solve this. Thanks a lot in advance.

CodePudding user response:

instead of this passing data like this.

Get.toNamed("/order_details?cartOrder=$cartOrder");

Try this.

Get.toNamed("/order_details",arguments:[cartOrder]);

and for getting data on second screen

var args = Get.arguments; // this should be placed before build method


var cartOrder = args[0]; // this should be placed after build method

CodePudding user response:

Unlike Get.arguments, which returns dynamic, Get.parameters returns Map<String,String?>. Therefore your actual data is always String or null.

So you shouldn't use parameters to pass complex objects. Use arguments instead.

If you really want to pass objects via parameters, you can serialise and send it (toJson) and after receiving, de-serialise it. But I wouldn't recommend it.

  • Related