0

Ok so I am kind of new to flutter, but I trying to fetch and display items in a list using map.

class TransactionTable extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TransactionTableState();
  }
}

class TransactionTableState extends State {
  final List moneyTransactions = [
    new MoneyTransactionModel(
        id: "1",
        productId: "lime",
        entryType: "sold",
        quantity: "60kg",
        unitPrice: "240rwf/kg",
        paidBy: "mtn",
        createdAt: "2020-04-20T00:15:08.932Z"),
    new MoneyTransactionModel(
        id: "2",
        productId: "lime",
        entryType: "sold",
        quantity: "60kg",
        unitPrice: "240rwf/kg",
        paidBy: "mtn",
        createdAt: "2020-04-20T00:15:08.932Z"),
    new MoneyTransactionModel(
        id: "3",
        productId: "lime",
        entryType: "sold",
        quantity: "60kg",
        unitPrice: "240rwf/kg",
        paidBy: "mtn",
        createdAt: "2020-04-20T00:15:08.932Z"),
    new MoneyTransactionModel(
        id: "4",
        productId: "lime",
        entryType: "sold",
        quantity: "60kg",
        unitPrice: "240rwf/kg",
        paidBy: "mtn",
        createdAt: "2020-04-20T00:15:08.932Z"),
  ];
 
  Widget build(BuildContext context) {
    // return 
    return Column(
    children: <Widget>[
      moneyTransactions.map((e) => MyListTile()); // i need to the the loop here
    ],
    );
  }
}

MyListTile is a widget supposed to display one item in the moneyTransactions list

2 Answers 2

1

You are inserting a List into an existing List, your build method should look like this:

Widget build(BuildContext context) {
    return Column(
        children: moneyTransactions.map((e) => MyListTile()).toList();
    );
}

Now for each item in the List, one instance of MyListTile is generated.

To have actual access to the information of the MoneyTransaction you would have to pass the object as a parameter in the constructor of MyListTile.

Sign up to request clarification or add additional context in comments.

Comments

1

You are nesting to lists and that's why it is not working. Also, you need to call toList() after the map() operation. Try following code:

Method 1

Widget build(BuildContext context) {
  // return 
  return Column(
    children: moneyTransactions.map((e) => MyListTile()).toList(),
  );
}

Method 2

Widget build(BuildContext context) {
  return Column(
  children: <Widget>[
      ...moneyTransactions.map((e) => MyListTile()).toList(),
    ],
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.