Md. Arian Rahman
Md. Arian Rahman

Reputation: 45

Flutter - The Method '+' was called on Null

Working on a Personal finance app , coding along to be more precise . The getter is supposed to return a list of days with keys assigned to each. When i run the code ,it's supposed to print out the empty list , Except it doesn't and when I add a new transaction , this error shows up in emulator

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

I don't have the slightest clue what's wrong . Here's the class that's causing the problem ,

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>[
        
      ],),
    );
  }
}

Upvotes: 2

Views: 1318

Answers (1)

timilehinjegede
timilehinjegede

Reputation: 14053

You are getting the error because you didn't give your totalSum variable an initial value.

double totalSum = 0; will fix the error.

Upvotes: 3

Related Questions