argument type 'String?' can't be assigned to the parameter type 'String' - flutter

The argument type 'String?' can't be assigned to the parameter type 'String' i got this error but previously the code was working fine. i have seen people raising the same issue but am not understanding the root cause. the package am using is flutter_audio_query nullsafe version from github. can someone help me with the issue.

String? which is nullable and you cannot pass directly to the non-null fields.
So use like this,
await player.setUrl(songInfo.uri ?? ''); // if null then empty string will be passed.
or
await player.setUrl(songInfo.uri!); // this is you saying that it won't be null.

Related

Why getting The method 'contains' isn't defined for the type 'String'. flutter

I am using contains method for a string in dart but getting an error like this.
can anyone tell me what is wrong with using .contains for a string in dart
This is my code
String isPdf = "https://www.africau.edu/images/default/sample.pdf";
isPdf.contains("pdf");
Error occurs
The method 'contains' isn't defined for the type 'String'.
Try correcting the name to the name of an existing method, or defining a method named 'contains'.
In vscode quickfix tells me that
change the 'contains' method to 'oscontains'.

The argument type 'BuildContext?' can't be assigned to the parameter type 'BuildContext'. The method 'read' isn't defined for the type 'BuildContext'

I have tried many ways and still can't fix it
The argument type 'BuildContext?' can't be assigned to the parameter type 'BuildContext'. The method 'read' isn't defined for the type 'BuildContext'.
It is saying that, scaffoldState.currentContext can return null. you can do a null check and then use it like
if(scaffoldState.currentContext != null)ScaffoldMessenger.of(scaffoldState.currentContext)...
I dont prefer using ! directly.
I think you can use the parameter context.
ScaffoldMessenger.of(context)...

Null check operator used on a null value instagram clone

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast
This might have basically happened while parsing json data from server into dart object. The issue basically arises in general whenever the members of the dart class are not nullable in which case they can't be left null at the time of initialisation. Due to which while you use the fromJson factory or function the value assigned to this parameter doesn't have that key available in it as a consequence you receive this error.
This could also arise while using named pushing of routes via arguments.

Cannot convert type value of type Observable<Store> to expected argument type Observable<Store?>

I'm getting an error which says the following:
Cannot convert value of type 'Observable<Store>' to expected argument type 'Observable<Store?>
So the method I'm calling StoreMock(state: stateSubject, store: storeSubject) seems to expect an optional for the observable storeSubject. But I'm not sure why it should matter if I pass in a non-optional. Shouldn't it be fine as it will always be there.
Any ideas?
You have to wrap the object into an Optional:
StoreMock(state: stateSubject, store: storeSubject.map(Optional.some))
Just one call will fix this.

How do I correctly cast a dynamic list to a List<List<String>> without runtime errors when .cast() has no effect?

I'm receiving data in a MethodCall object, which means that I cannot receive it as any type other than dynamic:
dynamic listOfObjects = methodCall.arguments;
but because I am the one sending the data from the platform-specific code, I know that the data is guaranteed to be of type List<List<String>>.
I want to inflate this data into a collection of concrete Dart object types:
List<DartObject> dartObjects =
methodCall.arguments.map((raw) => DartObject(
prop1: raw[0],
prop2: raw[1],
prop3: raw[2],
)).toList();
but this code fails with this error:
type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast
I've tried extensively to solve this issue on my own:
Dart's own documentation on fixing common type problems,
responses to similar Flutter issues, and
answers to similar Stack Overflow questions
all say to use the List's cast() method, but even this falls over at runtime with the same error:
(call.arguments as List).cast<List<String>>()
// => type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast
I'm sure I must be missing something obvious at this point. What am I doing wrong?
In short, how do I correctly cast to List<List<String>> without copying everything into a new data structure (i.e. without the use of .from or .map)?
The following code should work:
(call.arguments as List<dynamic>).map((e) => (e as List<dynamic>).cast<String>())