简体   繁体   中英

The operator '[]' isn't defined for the type 'Transaction'. Try defining the operator '[]'

class _UserTransactionsState extends State<UserTransactions> {
  final List<Transaction> _userTransactions = [
    Transaction(
      id: 't1',
      title: 'gasoline',
      amount: 10,
      date: DateTime.now(),
    ),
    Transaction(
      id: 't2',
      title: 'Gasoline',
      amount: 15,
      date: DateTime.now(),
    ),
  ];
   
    int qindex =0;
    int totalamount =0;
  void _sum_all_transactions() {
    setState(() {
      qindex = qindex + 1;
    });
    totalamount += Transaction[qindex]['amount'] as int; 
  }

i have a list and I want to sum amount values inside it but i receive the error.

The operator '[]' isn't defined for the type 'Transaction'. Try defining the operator '[]'.

thanks

You have a typo in your code:

totalamount += Transaction[qindex]['amount'] as int; 

Must be:

totalamount += _userTransactions[qindex].amount; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM