Home > OS >  I need this object inside the map, but i cant get it
I need this object inside the map, but i cant get it

Time:06-24

I have this list of maps, and i need to get the 'transactions' value:

final assets = [
    {
    "name": "BRL", "balance": 150.2, 
    "transactions" : Transaction(from: "someone", amount: 1)
    },
    {
    "name": "US", "balance": 1100.2, 
    "transactions" : Transaction(from: "someone", amount: 5)
    },
  ];

i tried to do assets[0]['transactions'], but all i got was null or Instance of 'Transaction'

the class:

class Transaction {
  final String id = Random().toString();
  final String from;
  final double amount;

  Transaction({
    required this.from,
    required this.amount,
  });
  
}

im kinda newbie, then pls help me :)

CodePudding user response:

You can use this dartpad example to view this in an app

Assuming you would like to show your data in a list (since this is a list of maps per the question)

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        shrinkWrap: true,
        itemCount: assets.length,
        itemBuilder: (context, index) {
          Transaction data = assets[index]['transactions'];
          return 
            ListTile(
              leading: Text(data.amount.toString()),
              title: Text(data.from.toString()));
        });
  }
}
  •  Tags:  
  • dart
  • Related