Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter Table.fromTextArray iterating a list while creating PDF

Hello everyone i was just wondering how should i iterate inside Table.fromTextArray which can be found in this package

https://pub.dev/packages/pdf

The Documentation is just very bad from my point of view and i was unable to locate any information that will help me achieve my goal

here is the code

   Container(
     margin: EdgeInsets.fromLTRB(22, 5, 22, 5),
     child: Directionality(
       textDirection: TextDirection.rtl,
       child: Table.fromTextArray(
           headers: <dynamic>['الإجمالي', 'العدد' ,'الخدمة', 'القطعة'],
           cellAlignment: Alignment.center,
           data: <List>[
             [
               '${purchases[0]['total_price']}',
               '${purchases[0]['quantity']}' ,
               '${purchases[0]['service_name_ar']}',
               '${purchases[0]['article_name_ar']}',
             ]
           ]
       ),
     ),
   )

as you can see i am printing the first item in the list using the [0] method i want to do it as a foreach how should i do that ?

Thanks in Advance

UPDATE Working Code:

  var purchasesAsMap = <Map<String, String>>[
    for(int i = 0; i < purchases.length; i++)
      {
        "total_price": "${purchases[i]['total_price']}",
        "quantity": "${purchases[i]['quantity']}",
        "service_name_ar": "${purchases[i]['service_name_ar']}",
        "article_name_ar": "${purchases[i]['article_name_ar']}",
      },
  ];

  List<List<String>> listOfPurchases=List();
  for(int i=0;i<purchasesAsMap.length;i++)
  {
    listOfPurchases.add(purchasesAsMap[i].values.toList());
  }

in build

Table.fromTextArray(
                   headers: <dynamic>['الإجمالي', 'العدد' ,'الخدمة', 'القطعة'],
                   cellAlignment: Alignment.center,
                   data: listOfPurchases
               ),
like image 324
RYOK Avatar asked Sep 21 '26 16:09

RYOK


1 Answers

Try this :

data:<List>[
             [
               for(var value in purchases[0].values) '$value'
             ]
           ]

Edit :

So after little head scratching I came to this :

void main()
{
  
  var listOfMap= <Map<String,String>>[{"name":"abcde","age":"21"},{"name":"xyz","age":"168"},{"name":"iam","age":"12"},{"name":"pqr","age":"1001"}];
  
  List<List<String>> listOfLists=List();
  

  for(int i=0;i<listOfMap.length;i++)
  {

      listOfLists.add(listOfMap[i].values.toList());
  }

 

  
  print(listOfLists.toString());
  
}

// Console :
// [[abcde, 21], [xyz, 168], [iam, 12], [pqr, 1001]]

Taking inspiration from above code you can modify your code accordingly , similar to this:

List<List<String>> providesListOfLists()
{
 List<List<String>> listOfLists=List();
  

  for(int i=0;i<purchases.length;i++)
  {

      listOfLists.add(purchases[i].values.toList());
  }

return listOfLists;
}

And in your build method call the function :

data:providesListOfLists,
like image 188
Anurag Tripathi Avatar answered Sep 25 '26 20:09

Anurag Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!