'The argument type 'Object?' can't be assigned to the parameter type 'num' dartargument_type_not_assignable

i was following a toturial and basically i got an error in this line and i dont know how to solve it. This widget is responsable for list the transactions that the user made. And the "item" and "data" are not working like they should. I think that its an error about the version of flutter because the toturial was old.

enter image description here

enter image description here

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../modelos/transaction.dart';
import './chart_bar.dart';


class Chart extends StatelessWidget {
  final List<Transaction> recentTransactions;

  Chart(this.recentTransactions);

  List<Map<String, Object>> get groupedTransactionValues {
    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,
      };
    }).reversed.toList();
  }

  double get totalSpending {
    return groupedTransactionValues.fold(0.0, (sum, item) {
      return sum + item['amount'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 6,
      margin: EdgeInsets.all(20),
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: groupedTransactionValues.map((data) {
            return Flexible(
              fit: FlexFit.tight,
              child: ChartBar(
                data['day'],
                data['amount'],
                totalSpending == 0.0
                    ? 0.0
                    : (data['amount'] as double) / totalSpending,
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}


Solution 1:[1]

It seems that the first argument of the ChartBar widget is expecting String, but you are passing data['day'] which returns Object? - that's what the error states.

To fix the problem, you could cast the value (only if you are sure that data['day'] returns a value):

ChartBar(
  data['day'] as String, // <-- Cast the value
  data['amount'],
  totalSpending == 0.0
    ? 0.0
    : (data['amount'] as double) / totalSpending,
),

If you have a default value for data['day'], you could go with the safer option:

ChartBar(
  data['day'] ?? 'Default', // <-- Using the null-coalescing operator
  data['amount'],
  totalSpending == 0.0
    ? 0.0
    : (data['amount'] as double) / totalSpending,
),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkobuolys