Search code examples
flutterfirebasegoogle-cloud-firestoreflutter-getx

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


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.


Solution

  • 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