Home > Net >  which type should i use here? what data is this snapshot is capturing?
which type should i use here? what data is this snapshot is capturing?

Time:06-18

I am unable to understand which circles data type I should change.

screenshot

    'https://pastebin.com/2cncFT38'

CodePudding user response:

The difference between List<Circles>? and List<Circles> is that the first one is nullable. This is because you have null safety in Dart.

With it, variables cannot be null if they haven't got a ? at the end.

You can use a null check ! as:

CirclePainter(circles: circles!) //If circles is null, an Exception will be thrown.

or use a ternary operator

CirclePainter(circles: circles ?? []) //If circles is null, an empty List will be returned

CodePudding user response:

The problem is related to Null-Safety not directly to the data type.

After Null-Safety, every type become has two types, a Nullable Type, which accepts a value or null and marked with a question mark at the end ?, and a Non-Nullable Type which accepts only a value and not null.

Your problem is that you are trying to pass a Nullable List<Circle>? to a Non-Nullable List<Circle> which is not allowed because the Nullable Type might contain a null value which is not accepted by the Non-Nullable Type while the vice versa is a accepted.

The solution is to check the value of circles and to make sure it is not null, this approach is called Type Promotion, you can do that by make a loading according to snapshot.connectionState and then use the bang operator ! to till Dart that you are sure the value is not null like the following circles!

  • Related