Home > database >  The method ' ' can't be unconditionally invoked because the receiver can be 'nul
The method ' ' can't be unconditionally invoked because the receiver can be 'nul

Time:02-06

I am having a problem with NonNullable feature of Dart

double? totalSum;

this is my variable that I will initialize later I used late key word also it also doesn't work

Here is the variable assigned

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

Error is on this line enter image description here

suggesting enter image description here

if I remove ? from my variable assign section it will give error saying Non Nullable must be assigned before using

CodePudding user response:

Are you initializing the value anywhere before the call ? Otherwise you are just adding to null on the first call. If you are initializing with a non null value before that line somewhere, you call add ! to your totalSum on the line where you use =.

Otherwise as Josteve said, just mark as non nullable with an initial value.

CodePudding user response:

Try this:

double totalSum = 0;

CodePudding user response:

Try declaring totalSum as non-nullable like:

double totalSum = 0;

or

totalSum!  = recentTransactions[i].amount;
  •  Tags:  
  • Related