7

In dart sdk greater than 2.12.0 we are often using '?' to make sure that argument can have null values also, but how to convert datatype with ? to datatype without ? or vice versa. what can be the most preferred way to sort out 'int?' can't of type 'int'.

Rishabh Bhardwaj
  • 365
  • 1
  • 5
  • 11

6 Answers6

12

You get this error on assigning a nullable string (String?) to a non-nullable string (String). For ex:

String? s = 'a';

int expectsString(String s) => s.length;

void main() {
  expectsString(s); // <-- Error
}

The solution is to either provide a non-nullable String. You can do that using:

(1) Local variable:

void main() {
  var s1 = s; // Local variable 
  expectsString(s1 ?? 'default value');
}

(2) Bang operator:

void main() {
  expectsString(s!); // Bang operator
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
2

The null safety is a method to say that a type can or can not be null, you are not wrapping anything inside an object, so you don't have anything to unwrap.

Maybe you are confusing this with the optional type from functional language or Java. Where an optional type can be unwrapped if it contains a value.

With dart, google take another decision, to give the possibility to the user to write a clean code without check if an object is null and vice versa, so with null safety you have the mathematical proof that your code doesn't access to null reference.

So, if you need to unwrap the null object or you don't need it, or you need a check like that

foo(int? i) {
  if (i != null) {
    print(i + 1);
  }
}

I want to point out a great reference of one dart developer that describe why to google take this decision https://medium.com/dartlang/why-nullable-types-7dd93c28c87a

vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35
2

I had a similar issue in one of my flutter apps, and I had fixed it with type casting from String? to String.

just like in the code below:

// I'm using a List with one element to generate the same error,
// because the error doesn't happen for one single variable.

List<String?> imageUrls = ['https://www.example.com/example_image.png',];

// you cast the element imageUrls[0] with 'as' keyword from String? to String

NetworkImage(imageUrls[0] as String); // NetworkImage() takes String parameter
OMBHD
  • 21
  • 4
1

You can cast the type like this:

int? a = 2;
int b;
b = a.toInt();

or

int? a = 2;
int b;
b = (a as int);

Same with string, we have .toString() and as String.

Adib Zaini
  • 119
  • 2
  • 8
0

When dealing with string, I would instantiate the variable with an empty string.

e.g. rather than writing

String? a; I would go with

String a = ""; It is not ideal, but it is a temporary solution, especially when using the variable a as a named constructor.

With this I can peacefully use a as a variable without the concerns of String? not being the same type as String.

-1

if the compiler knows that the nullable variable is initialized you can give it to not nullable variable, for example, this works:

String? a; //a is nullable
a = 'some value'; // a is initialized

String b = ''; // b is not nullable
b = a; //b can receive a once a is initialized

Thats be cause the compiler knows that a is not null, but if a was not initialized this would not works.

Jorge Vieira
  • 2,784
  • 3
  • 24
  • 34