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

Time:03-23

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

Dart supports sound null safety. Try using something like

String nonNullableString = nullableString ?? 'default';

OR

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

  • Related