【问题标题】:The argument type 'Object?' can't be assigned to the parameter type 'String'参数类型“对象?”不能分配给参数类型“字符串”
【发布时间】:2021-07-17 16:51:12
【问题描述】:

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

不知道哪里出错了

【问题讨论】:

  • 顺便说一句,这段代码来自 udemy 课程。 :) 我也面临同样的问题。

标签: flutter


【解决方案1】:

方法groupedTransactionValues 正在返回Object 的映射。相反,返回dynamic 的映射,它是运行时确定的类型。换行:

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

到:

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

【讨论】:

【解决方案2】:

我有一个错误:

The argument type 'Object' can't be assigned to the parameter type 'Error'.

在示例中:

try {
  ...
} catch (e) {
  yield OnePageLoadFailed(error: e);
}

它在 catch 中给出了一个错误,因为它是 Object 类型,而我的 OnePageLoadFailed 需要 Error 类型。所以我就这样离开了:

} on Error catch (e) {
  yield OnePageLoadFailed(error: e);
}

我希望你能帮助解决别人的问题,包括我,尤其是在Null Safety 条目之后。 :D

【讨论】:

    【解决方案3】:
    double get totalSpending {
      return groupedTransactionValues.fold(0.0, (sum, item) {
      return sum + (item['amount'] as double); 
    }
    

    【讨论】:

    • 这到底是做什么的?你能解释一下吗?
    【解决方案4】:

    将对象更改为动态:

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

    如果错误没有解决,请尝试添加:

     data['day'] as String,
     data['amount'] as double,
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-10-13
      相关资源
      最近更新 更多