The argument type 'Object?' can't be assigned to the parameter type 'num' for getter (as double) is not working

Question

Asked by Mafruh on November 17, 2021 (source).

I am making a personal expense tracker app in Flutter. I want to output a percentage of daily expenses compared to the total weekly expense. So, I need to GET the total sum of the week in Double datatype through .fold() method. But the inspector shows "The argument type 'Object?' can't be assigned to the parameter type 'num'." I tired both as double and as int.

import 'package:flutter/material.dart';
import 'package:flutter_app_module3/models/transaction.dart';
import 'package:intl/intl.dart';
import './chart_bar.dart';

class Chart extends StatelessWidget {

  final List<Transaction> recentTransaction; 
  

  Chart(this.recentTransactions);

  List<Map<String, Object>> get groupedTrandactionValues {
    return List.generate(7, (index) {
      final weekDay = DateTime.now().subtract(
        Duration(days: index),
      );
      var totalSum = 0.0;

      for (var i = 0; i < recentTransactions.length; i++) {
        if (recentTransactions[i].date.day == weekDay.day &&
            recentTransactions[i].date.month == weekDay.month &&
            recentTransactions[i].date.year == weekDay.year) {
          totalSum += recentTransactions[i].amount;
        }
      }

      return {
        "Day": DateFormat.E().format(weekDay).substring(0, 1),
        'amount': totalSum,
      };


  double  get totalSpending {
    return groupedTrandactionValues.fold(0, (prev, element) {
     
      return prev + element['amount'];
    });
  }

  @override
  Widget build(BuildContext context) {
    print(groupedTrandactionValues);
    return Card(
      child: Row(),
    );
  }
}

groupTransactionValues returned:

  [{"Day": "W", "amount": 86.52},
           {"Day": "T", "amount": 45.0},
           {"Day":" M"," amount": 23.0}, 
           {"Day": "S", "amount": 45.0},
           {"Day": "S", "amount": 67.0}, 
           {"Day": "F", "amount": 98.0}, 
           {'Day': "T", "amount": 45.0}];
            });
          }
    

I tried as double

double  get totalSpending {
    return groupedTrandactionValues.fold(0.0, (prev, element) {
     
      return prev + element['amount'] as double;
    });
  }

Also as int:

int  get totalSpending {
    return groupedTrandactionValues.fold(0, (prev, element) {
    
      return prev + element['amount'] as int;
    });
  }

Answer

Question answered by Taz (source).

Change
List<Map<String, Object>> get groupedTrandactionValues
to
List<Map<String, dynamic>> get groupedTrandactionValues

DART FLUTTER
SHARE: