【问题标题】:Flutter - The Method '+' was called on NullFlutter - 在 Null 上调用了方法“+”
【发布时间】:2020-07-19 15:28:49
【问题描述】:

在个人理财应用上工作,编码更精确。 getter 应该返回一个天数列表,其中每个天数都分配了键。 当我运行代码时,它应该打印出空列表,除非它没有,当我添加一个新事务时,这个错误会出现在模拟器中

NoSuchMethodError: The methos '+' was called on Null. Receiver: null. Tried Calling: +(100)

我一点也不知道出了什么问题。 这是导致问题的类,

import 'package:demo_app/models/transaction.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.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;
      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) , 'amount': totalSum }; //this is a map
    });
  }
  @override
  Widget build(BuildContext context) {
   print(groupedTransactionValues);
    return Card(
      elevation: 5,
      margin: EdgeInsets.all(20),
      child: Row(children: <Widget>[
        
      ],),
    );
  }
}

【问题讨论】:

  • 给你的totalSum变量一个初始值。 double totalSum = 0.
  • 成功了!请将其发布为答案,以便我可以关闭它。

标签: android flutter dart


【解决方案1】:

您收到错误是因为您没有为 totalSum 变量指定初始值。

double totalSum = 0; 将修复错误。

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 2019-09-23
    • 2020-06-04
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2021-09-29
    • 2020-04-27
    相关资源
    最近更新 更多