gpt4 book ai didi

flutter - 参数类型 'Object?' 不能分配给参数类型 'String'

转载 作者:行者123 更新时间:2023-12-04 12:13:57 25 4
gpt4 key购买 nike

These are the errors I get
这是我的 chart.dart 文件

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()));
}
}
我不知道我哪里错了

最佳答案

方法groupedTransactionValues正在返回 Object 的 map .相反,返回 dynamic 的 map 这是运行时确定的类型。更改行:

List<Map<String, Object>> get groupedTransactionValues {
到:
List<Map<String, dynamic>> get groupedTransactionValues {

关于flutter - 参数类型 'Object?' 不能分配给参数类型 'String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67230640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com