Home > Mobile >  The argument type 'Object?' can't be assigned to the parameter type 'num' f
The argument type 'Object?' can't be assigned to the parameter type 'num' f

Time:11-17

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;
    });
  }

CodePudding user response:

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

CodePudding user response:

When getting an element from a map element['amount'] it can be null, so do this.

return prev   (element['amount'] ?? 0.0);

This means if the amount in element is null for some reason it'll add 0.0

Edit: This works for me in dartpad

  List<Map<String, Object>> data = [
    {"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},
  ];

  double totalSpending() {
    return data.fold(0.0, (double prev, Map<String, Object> element) {
      return prev   (element['amount'] as double? ?? 0.0);
    });
  }

  var total = totalSpending();
  print(total);

409.52

(element['amount'] as double? ?? 0.0) means get the amount as a double or null, if it is null use 0.0

  • Related