'The argument type 'Object?' can't be assigned to the parameter type 'String'
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
Solution 1:[1]
Change the object to dynamic in:
(List<Map<String, Object>> get groupedTransactionValues { )
If the error is not solved, try adding:
data['day'] as String,
data['amount'] as double,
Solution 2:[2]
I had a error:
The argument type 'Object' can't be assigned to the parameter type 'Error'.
In the example:
try {
...
} catch (e) {
yield OnePageLoadFailed(error: e);
}
And it gave an error in the catch because the is of type Object, and my OnePageLoadFailed expects the type Error. So I left it like this:
} on Error catch (e) {
yield OnePageLoadFailed(error: e);
}
I hope you can help solve someone's problem, as well as me, especially after the Null Safety entry. :D
Solution 3:[3]
double get totalSpending {
return groupedTransactionValues.fold(0.0, (sum, item) {
return sum + (item['amount'] as double);
}
Solution 4:[4]
The argument type 'Object?' can't be assigned to the parameter type 'String'. Therefore, you have to change it like the following so it is treated as String and double, respectively. Instead of
return sum + item['amount'];
use
return sum + (item['amount'] as double);
Likewise, instead of
data['day']
use
(data['day'] as String)
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 | ouflak |
| Solution 2 | Felipe Sales |
| Solution 3 | General Grievance |
| Solution 4 | Ali Bajwa |
