【问题标题】:The operator '/' isn't defined for the type 'Set<double>'. Try defining the operator '/'. Quiz app没有为类型“Set<double>”定义运算符“/”。尝试定义运算符'/'。测验应用程序
【发布时间】:2021-12-02 21:07:52
【问题描述】:

我收到三个错误:

  1. 参数类型“对象?”不能分配给参数类型“num”。行 - 返回总和 + item['amount'];
  2. 没有为“Set”类型定义运算符“/”。 尝试定义运算符'/'。行 - """ {data['amount'] as double} / totalSpending),"""
  3. 参数类型“对象?”不能分配给参数类型'double'.Line - """data['amount'],"""

我尝试使用双重解析,但它只解决了第三个错误,然后给出了错误: .参数类型“对象?”不能赋值给参数类型'string'

前两个我无法完全解决。

    import 'package:expense_app/Widget/chart_bar.dart';
import 'package:expense_app/models/transaction.dart';
import 'package:flutter/material.dart';

import '../models/transaction.dart';
import 'package:intl/intl.dart';

import '../Widget/chart_bar.dart';

class Chart extends StatelessWidget {
  //const Chart({ Key? key }) : super(key: key);
  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; //using double instead of var to initiliaze it
      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;
        }
      }

      //print(DateFormat.E().format(weekDay));
      //print(totalSum);
      return {
        'day': DateFormat.E()
            .format(weekDay)
            .substring(0, 1), //shortcut or giving us weekdays
        'amount': totalSum,
      };
    }).reversed.toList(); //This will give us a list with 7 elements
  }

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

  @override
  Widget build(BuildContext context) {
    print(groupedTransactionValues);
    return Card(
      elevation: 6,
      margin: EdgeInsets.all(20),
      child: Container(
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: groupedTransactionValues.map((data) {
            return Flexible(
              fit: FlexFit
                  .tight, //the child can grow and take extra space from others making the look clumsy with larger values
              child: Chartbar(
                  data['day'].toString(),
                  data['amount'],
                  totalSpending == 0.0
                      ? 0.0
                      : {data['amount'] as double} / totalSpending),
            );
          }).toList(),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 你能告诉我们每一行的错误吗?如此有限的信息很难为您提供帮助。
  • 嘿,我已经添加了出现这些错误的行

标签: flutter flutter-layout


【解决方案1】:

尝试以下更改:

投射你的item['amount']的类型。

return sum + (item['amount'] as double);

也投到这里

              child: Chartbar(
                  data['day'].toString(),
                  data['amount'] as double,

用普通括号替换花括号。

: (data['amount'] as double) / totalSpending)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2021-04-03
    • 1970-01-01
    • 2021-06-11
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多