0

After defining a map (with letters as keys and scrabble tile scores as values)

Map<String, int> letterScore //I'm omitting the rest of the declaration

when I experiment with this function (in DartPad)

int score(String aWord) {
  int result = 0;
  for (int i = 0; i < aWord.length; ++i) {
    result += letterScore[aWord[i]];
  }

  return result;
}

I consistently get error messages, regardless of whether I experiment by declaring variables as num or int:

Error: A value of type 'int?' can't be assigned to a variable of type 'num' because 'int?' is nullable and 'num' isn't [I got this after declaring all the numerical variables as int]

Error: A value of type 'num' can't be returned from a function with return type 'int'.

Error: A value of type 'num?' can't be assigned to a variable of type 'num' because 'num?' is nullable and 'num' isn't.

I understand the difference between an integer and a floating point (or double) number, it's the int vs int? and num vs num? I don't understand, as well as which form to use when declaring variables. How should I declare and use int or num variables to avoid these errors?

julemand101
  • 28,470
  • 5
  • 52
  • 48
Al C
  • 5,175
  • 6
  • 44
  • 74
  • Your problem is that the `[]` operator on a `Map` returns a value which can be `null`. The reason is that if you are requesting something in your `Map` which does not exists, it will return `null`. So in your case, `letterScore` will return a `int?` when using the `[]` operator. – julemand101 May 11 '21 at 18:40
  • You are therefore required to handle the case there `[]` returns `null`. Or you can use `letterScore[aWord[i]]!` (see the `!` sign) which will force the analyzer to stop complaining and should just see the value as non-nullable. But you will then get a runtime exception in case of `[]` returning `null`. – julemand101 May 11 '21 at 18:42

1 Answers1

2

Take this for example:

int x; // x has value as null
int x = 0; // x is initialized as zero

Both the above code are fine and compilable code. But if you enable Dart's null-safety feature, which you should, it will make the above code work differently.

int x; // compilation error: "The non-nullable variable must be assigned before can be used"
int x = 0; // No Error.

This is an effort made from the compiler to warn you wherever your variable can be null, but during the compile time. Awesome.

But what happens, if you must declare a variable as null because you don't know the value at the compile time.

int? x; // Compiles fine because it's a nullable variable

The ? is a way for you tell the compiler that you want this variable to allow null. However, when you say a variable can be null, then every time you use the variable, the compiler will remind you to check whether the variable is null or not before you can use it.

Hence the other use of the ?:

int? x;
print(x?.toString() ?? "0");

Further readings:

Official Docs: https://dart.dev/null-safety/understanding-null-safety

Null-aware operators: https://dart.dev/codelabs/dart-cheatsheet

lrn
  • 64,680
  • 7
  • 105
  • 121
Sisir
  • 4,584
  • 4
  • 26
  • 37