Required Set<String> but 'of' was inferred to ImmutableSet<E> - guava

I am trying to pass in a Set<String> in a constructor but I'm getting the following error:
Incompatible types. Required Set<String> but 'of' was inferred to ImmutableSet<E>: no instance(s) of type variable(s) E exist so that ImmutableSet<E> conforms to Set<String>
Here is the code:
final CustomMetricsModule customMetricsModule = new CustomMetricsModule(Collections.EMPTY_MAP,
ImmutableSet.of("val", "val1"));
CustomMetricsModule has a map and Set<String> as its params.

Issue was fixed by changing the Set that was imported. In CustomMetricsModule, I was supposed to be importing java.util.Set, but I wasn't.

Related

Flutter ignore field using Freezed

I'm using Freezed to generate my models. I want to ignore a specific key, so I'm using #JsonKey(ignore: true).
#freezed
class ObjectA with _$ObjectA {
const factory ObjectA({
#JsonKey(ignore: true)
List<ObjectB> ObjectBList,
}) = _ObjectA;
factory ObjectA.fromJson(Map<String, dynamic> json) => _$ObjectA(json);
}
When I try to run the build_runner, I'm getting the above exception:
The parameter ObjectBList of ObjectA. is non-nullable but is neither required nor marked with #Default
Why doesn't it been ignored?
The issue you are facing is with Dart syntax. When you declare parameters in constructors.
You can have a nullable parameter or a non-nullable parameter; this also apply to variables.
If you want the parameter you declared in the constructor to be non-nullable then it must either have a default value or the declared parameter must be prefixed with the required keyword.
You have three options:
Add the required keyword
required List<ObjectB> ObjectBList
Make it nullable
List<ObjectB>? ObjectBList
Give it a default value. With freezed this can be done with the #Default annotation
#Default([]) List<ObjectB> ObjectBList

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<X>

Shouldn't we be able to assign a value to a field IF IT IS THE SAME TYPE UNDER THE HOOD - E.G. IN THE DEBUGGER IT SAYS IT IS A List<GroceryItmTag>? and you are trying to assign it to a List<GroceryItmTag>?, and just cast it to the correct type or to dynamic? Here I am hovering over formFieldValue, which I later assign to groceryItm.tags:
groceryItem.tags is List<GroceryItmTag>? and I'm assigning to it, a field which is a List<GroceryItmTag>?, under the hood, even though it is not recognised as that. But it is throwing this exception whether I cast it as List<GroceryItmTag>? or I just cast it to dynamic and assign it.
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<GroceryItmTag>?'
How can I assign it without throwing the exception?
This is hovering over groceryItm.tags, the field that I am trying to assign formFieldValue to {List<GroceryItmTag>? tags}:
Shouldn't we be able to assign a value to a field if it is the same type under the hood, and just cast it to the correct type or to dynamic?
No
Simple example. If I have a List<Sheep> and I assign it to a List<dynamic>, now you could suddenly insert a Wolf() into my List<Sheep> through that List<dynamic> variable. Because a Wolf is a dynamic, too and you can insert into a List<dynamic>. That is why it does not work and will not work.
You could assign your List<Sheep> to a plain dynamic though and cast it as neccessary.
A workaround is to filter on the type and assign that. Example:
List<dynamic> list = ['a', 'b', 'c'];
List<String> stringList = list as List<String>; //this will give a runtime error
List<String> stringList = list.whereType<String>().toList(); //workaround

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

when I upgrade my flutter to 2.0.1, shows this error:
The argument type 'String?' can't be assigned to the parameter type 'String'.
this is my code:
enum SubStatus {
SUB,
UNSUB,
}
extension ResponseStatusExtension on SubStatus{
static const statusCodes = {
SubStatus.SUB: "sub",
SubStatus.UNSUB: "unsub",
};
String? get statusCode => statusCodes[this];
}
This is how to use it:
String url = "/post/sub/source/" + subStatus.statusCode + "/" + channelId;
this is the error UI:
what should I do to fix it? I tried to return String but in the enum code tell me should return String?:
what should I do?
Change the return type of statusCode to String and provide a default value.
String get statusCode => statusCodes[this] ?? '';
When accessing a map, there is a chance that you will get a null return value if the key does not exist in the map. Simply providing a default value will allow this code to compile. That default value should never be used unless you add something to the enum without adding a value to the map as well.
Edit:
After the comment from #Christopher Moore, I realized my mistake. So, I am going to directly use his solution over here as it is the correct one.
This is because of the new null-safety feature of Dart.
You will need to make the following change in the code and it will work:
String get statusCode => statusCodes[this] ?? '';
With new null-safety rules, the following data-type? x, the data type is followed by a question mark, means that the value x can be null. However, without the '?', it means that data-type x, it cannot be null.
So, basically String and String? are two different data types. That is why you get the error.
You can learn more here.
restart analysis server
add !
like this
subStatus.statusCode!

A value of type 'List<Customer>' can't be assigned to a variable of type 'Future<List<Customer>>'

How to convert 'List' to 'Future<List>'. I need get two type ('List' & 'Future<List>' ) in different places
My api response
var data = jsonDecode(r.body);
custList.add(new Customer(
data[i]['CustomerID'],
'${data[i]['ProfileImageUrl']}' + '${data[i]['ProfileImage']}',
'${data[i]['CompanyName']}',
data[i]['Email'],
data[i]['RegisterNumber'],
data[i]['PhoneNumber'],
data[i]['BillingStreet'],
data[i]['BillingCity'],
data[i]['BillingZip'],
data[i]['MasterCountryName'],
data[i]['MasterStateName'],
data[i]['Type'],
new customeraddress(data[i]['BillingStreet'],
data[i]['BillingCity'], data[i]['BillingZip']),
status,
data[i]['CustomerTaxExemptType'],
data[i]['MasterCountryID'],
data[i]['MasterStateID'],
));
How to convert 'List' to 'Future<List>'.
If you are not entirely sure if you get a T or a Future<T>, you can use the FutureOr<T> (documentation) class.
To create a Future<T> from a value to satisfy some compiler syntax, you can use the named constructor Future.value (documentation).
For more information on Futures: What is a Future and how do I use it?

The argument type 'List<String>' can't be assigned to the parameter type 'List<String>Function()'

I have a Map that consists of a String key and a List value. However when I try to add a new value to the Map, I get the error "The argument type 'List' can't be assigned to the parameter type 'ListFunction()'".
The sample code is as follows:
Map<String, List<String>> map = new Map<String, List<String>>();
String key = "Key1";
List<String> currentValues = [];
map.putIfAbsent(key,currentValues);
The last line throws the error above. Does anyone have a fix for this?
From the docs, putIfAbsent takes a function as a second argument. So you should write something like this :
map.putIfAbsent(key, () => currentValues);