Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

These are the errors I get

Here is my chart.dart file

import 'package:flutter/material.dart';
import 'package:udemy_expenses_app/widgets/chart_bar.dart';
import '../models/tanscations.dart';
import 'package:intl/intl.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,
    ),
  );
  double 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 groupedTransactionValues.fold(0.0, (sum, item) {
  return sum + item['amount']; 
  //  Error: A value of type 'Object?' can't be assigned to a variable of type 'num'.
 });
}

@override
Widget build(BuildContext context) {
  return Card(
    elevation: 6,
    margin: EdgeInsets.all(20),
    child: Row(
        children: groupedTransactionValues.map((data) {
      return ChartBar(
        data['day'],
        // Error: The argument type 'Object?' can't be assigned to the parameter type 'String'.
        data['amount'],
        // Error: The argument type 'Object?' can't be assigned to the parameter type 'String'.
        (data['amount'] as double) / totalSpending,
      );
    }).toList()));
  }
}

I dont know where did I go wrong

like image 586
Niranjan kumar Avatar asked Apr 23 '21 13:04

Niranjan kumar


People also ask

How can I resolve the argument type String can't be assigned to the parameter type URI flutter?

You can fix the mentioned issue with one line of code. By using the Uri. parse() method, you can create a new Uri object by parsing a URI string. Note that the Uri class comes with dart:core so you can use it right away.

What is covariant keyword in Dart?

The covariant keyword This removes the static error and instead checks for an invalid argument type at runtime.


1 Answers

The method groupedTransactionValues is returning a map of Object. Rather, return a map of dynamic which is a runtime determined type. Change the line:

List<Map<String, Object>> get groupedTransactionValues {

to:

List<Map<String, dynamic>> get groupedTransactionValues {
like image 136
Patrick O'Hara Avatar answered Nov 11 '22 14:11

Patrick O'Hara