(The argument type 'String?' can't be assigned to the parameter type 'String') What is the difference between String and String in dart? - flutter

lately i always get errors like:
(The argument type 'String?' can't be assigned to the parameter type 'String')
or (The argument type 'File?' can't be assigned to the parameter type 'File') and i don't know the difference between em or what to do.
help me please
this error made my life much harder this days please help guys

Since dart introduced sound null-safety a String variable can not be null,
but if your variable might be null you can declare it as String?.
Therefor you can not assign something that might be null(String?) to something null-safe(String).
Same with File? and File.
with ? at the end of a type you indicate that it might be null.
https://dart.dev/null-safety

Related

Is `!any` valid in JSDoc as the type of a parameter?

Is !any valid in JSDoc as the type of a parameter? And what would it mean?
I was trying to use this to constrain a variable to be of any type except 'null' or 'undefined'. But atleast in VSCode, it seems that this check is not being enforced.
You can use NonNullable utility type
/**#type {string | null | undefined}*/
let foo;
/**#type {NonNullable<typeof foo>}*/(foo).toLowerCase();
https://github.com/microsoft/TypeScript/issues/23405

How to use strings for OSLogMessage?

Given the following two lines, XCode is showing an error for the second line saying Cannot convert value of type 'String' to expected argument type 'OSLogMessage'.
logger.info("Device found:")
logger.info(String(describing: device))
Can someone please explain why this error is shown? In both cases, the parameter is of type String. (I guess)
Currently, I fix this by using string interpolation. But this does not feel right. Is there a better way than:
logger.info("\(String(describing: device))")

! casts the expression to its underlying non-nullable type in Flutter

What does it mean when they say:
A postfix exclamation mark (!) takes the expression on the left and casts it to its underlying non-nullable type.
https://dart.dev/null-safety/understanding-null-safety#null-assertion-operator
Does it mean that it checks whether the expression is null or not and then throws error before executing anything further?
Please explain this:
casts it to its underlying non-nullable type
Does it mean that it checks whether the expression is null or not and then throws error before executing anything further?
Yes, ! will insert a null-check that will throw a TypeError if the expression turns out to be null at runtime.
As stated in the last paragraph of the "Null assertion operator" section:
Of course, like any cast, using ! comes with a loss of static safety. The cast must be checked at runtime to preserve soundness and it may fail and throw an exception.
casts it to its underlying non-nullable type
It means that the variable might be nullable but when you are explicitly sure that the current call to the variable will not return null then you use the ! operator to imply that the variable is not null.
For example you have this:
List<ChatMessage?> messages;
This would be a list of messages. But they can be null.
The message has a body, id etc.
If I would try to call an id on a message that is inside the list as followed:
message.id
Message can be null in this case which will tell me that Message cannot be Null when calling id.
So what we do here is a ! to the end of message then it casts to its underlying type which can't be a null at this point(because we define it cannot be with the !) or it will throw an error:
messages!.id

A value of type 'List<int>' can't be assigned to a variable of type 'Iterable<int>'

I don't know if this was happening before my switch to the beta channel in Flutter but I don't understand why this is an error. A List is an Iterable right? I took the example from the official docs.
Iterable<int> example() {
Iterable<int> iterable = [1, 2, 3];
return iterable;
}
VSCode marks the list with a red underline telling me:
A value of type 'List<int>' can't be assigned to a variable of type 'Iterable<int>'.
Try changing the type of the variable, or casting the right-hand type to 'Iterable<int>'. dart(invalid_assignment)
I think the problem is the as is more of an assertion and not a cast. To perform the cast you need to use the .cast function:
https://api.flutter.dev/flutter/dart-core/List/cast.html
E.g.
# ...
.toList().cast<Iteratable<File>>()
You can read more from this answer covering when/why to cast: https://stackoverflow.com/a/49542401/931209
Alternatively, you can probably call the iterator property on the list (but the above is likely the more correct solution):
https://api.dart.dev/stable/2.10.4/dart-core/Iterable/iterator.html
I don't know what was the problem but I tried removing the Flutter and Dart extensions from VSCode, restarting the whole computer and installing them again (in that order) and now the same code works!
As BeefPapa said in a comment, the code was absolutely correct.
Who knows what happened.
You need convert again the results to List adding .toList() in the end because Dart changes the type of the variable.

Postgres: How to casting JSONB value to numeric

I am having issues casting a jsonb value. And would love some guidance.
what we are trying to achieve is that some data came in as strings, and we want to cast that to numbers.
Consider the following update statement:
update customer
set traits = jsonb_set(traits, '{arr}',traits->'arr'::text::integer)
where jsonb_typeof(traits->'arr') = 'string'
and traits->'arr' is not null
We currently get the following error:
[22P02] ERROR: invalid input syntax for type integer: "arr"
I have tried all sort of casting incantations, but can't figure away past this.
Anyone have a path forward for us ?!
working solution looks like THIS:
update customer
set traits = jsonb_set(traits, '{arr}',(traits->>'arr')::integer::text::jsonb)
where jsonb_typeof(traits->'arr') = 'string'
and traits->'arr' is not null
with a triple cast. Smells a bit off
The problem is that your expression
traits->'arr'::text::integer
is evaluated as
traits->('arr'::text::integer)
which is trying to cast 'arr' to an integer (failing for obvious reasons with the error message you mention). Instead, you want
(traits->'arr')::text::integer
-- or
(traits->>'arr')::integer