John Reaper
John Reaper

Reputation: 305

Error: A value of type 'String?' can't be assigned to a variable of type 'String'

Some one can help me. Error Message: A value of type 'String?' can't be assigned to a variable of type 'String'. Try changing the type of the variable, or casting the right-hand type to 'String'.

enter image description here

Upvotes: 3

Views: 4063

Answers (4)

Naveen Kulkarni
Naveen Kulkarni

Reputation: 803

Dart supports sound null safety. Try using something like

String nonNullableString = nullableString ?? 'default';

OR

actions=uri.base.queryParameters["key"]!

Upvotes: 1

Sagar Ghag
Sagar Ghag

Reputation: 155

As dart uses sound null safety, there are chances that the value in Uri.base.queryParameters["actions"] can be null. This is something that Flutter has adapted from the Swift programming language.

So basically, there are 2 ways you can solve this problem.

  1. Using the null check.
final String? actions = Uri.base.queryParameters["actions"];
if (actions == null) {
/// The value of [actions] is null
return;
}
/// Continue with your coding...
  1. By providing an optional value.
final String _actions = Uri.base.queryParameters["actions"] ?? "defaultValue";

I hope you understood what I am trying to say.

If you have any other doubts, do let me know.

Upvotes: 1

Mouaz M Shahmeh
Mouaz M Shahmeh

Reputation: 106

Use Null Safely code as this:

action = Uri.base.queryParameters['action']!; // add ! mark

The same process for all errors:

encryptedEmailAddress = Uri.base.queryParameters['encryptedEmailAddress']!; // add ! mark
doctorUID = Uri.base.queryParameters['doctorUID']!; // add ! mark

Upvotes: 1

Vishal_VE
Vishal_VE

Reputation: 2137

Check some points with null safety declaration of data member of class. And also check out the type of action parameter what it takes- It may take String type of data then update your variables/data member accordingly.

Please look into the concept of null safety.

Upvotes: 1

Related Questions