How to Resolve The Argument type 'String?' can't be assign to parameter type 'String'
Asked Answered
M

6

8

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'.

Mishnah answered 11/5, 2021 at 23:22 Comment(1)
You can ask with some codes as well as error you get. This is an incomplete question.Sporozoite
A
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
}
Authorization answered 15/5, 2021 at 15:44 Comment(0)
C
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

Context answered 11/5, 2021 at 23:42 Comment(0)
C
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
Calpe answered 16/6, 2021 at 10:13 Comment(2)
! is essentially an equivalent of as String.Authorization
you're right, it's just that as String is more readable than the ! bang operator, however the ! bang operator makes the code shorter !Calpe
G
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.

Giraldo answered 1/9, 2021 at 9:16 Comment(0)
H
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.

Hilary answered 22/6, 2021 at 15:6 Comment(0)
E
-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.

Evelineevelinn answered 11/5, 2021 at 23:42 Comment(1)
another choice is to check if the value is null, the compiler makes the code analysis and all of us we are happy and safe :)Context

© 2022 - 2024 — McMap. All rights reserved.