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

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);

Related

Unhandled Exception: type 'List<Widget>' is not a subtype of type 'Iterable<MyWidget>' in type cast

After migrating to null safety, the migration tool added as Iterable<SetWidget> to my code. But unfortunately i get this error:
Unhandled Exception: type 'List<Widget>' is not a subtype of type 'Iterable<SetWidget>' in type cast
This is the code:
final List<Widget> _sets = [];
Map getSets() {
Map sets = {};
int k = 0;
for (SetWidget set in _sets as Iterable<SetWidget>) {
sets.putIfAbsent(k.toString(), () => set.getMap());
k++;
}
return sets;
}
What is the issue here?
I don't know why the tool decided to do that, but if you think about it, it obviously throws an error. _sets is a list of Widgets, which means it could include for example a Column, a Container, and a SetWidget, so it can't be cast to a SetWidget iterable.
is getMap() a method in the SetWidget class? if it is, could you change the List<Widget> to a List<SetWidget> and remove the as Iterable<SetWidget>? Hopefully, that solves your problem.
Also, side note, what is this code for? Are you turning a list into a Map<String, Function> where the string is just an index? if you are, why not use a list instead?
for (SetWidget widget in _sets)
{
sets.add(widget.getMap)
}

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?

Flutter: How to declare HashMap of StreamSubscription

I have declared hash map for subscriptions, like this:
HashMap<String, StreamSubscription<Event>> = subscriptions
But when i am filling values, it show for me, that types doesn't match. Error tells that Argument of type StreamSubscription<Event> can't be assigned to parameter of type StreamSubscription<Event> Function().
When i am creating dirrectly variable like this:
StreamSubscription<Event> e = subscription.eventStream.listen((event) {
print(event.arguments[0]);
});
It shows that type is correct.
So how should i declare hashmap of HashMap<String, StreamSubscription<Event>>, if
_subscriptions.putIfAbsent(uri, e);
doesn't work.

Converting Set of a Map to a Map

I need help in converting an argument type from Set to a Map. I using batch update in my flutter code and '.toJson()' method to get the 'data'.
The warning I get is :
The argument type 'Set<Map<String,dynamic>>' can't be assigned to the parameter type 'Map<String,dynamic>'.
..for the 'data' part in batchUpdate command.
Is there any way to convert the set to a Map so batch.Update can accept it?
We may first convert your data from .toJson() method like this code below :
// 1. As explained, toJson() results `data` typed as Set<Map<String, dynamic>>
Set<Map<String, dynamic> data = payload.toJson()
// 2. Convert `data` type to Map<String, dynamic>
Map<String, dynamic> newData = data.single
// 3. then proceed `data` to next method
batchUpdate(data);

How to correctly do type conversions in dart

I'm building a flutter app that uses Dog API but I am failing to convert the type of a Map to Map<String, List<String>>
I have tried to use map.cast<String,List<String>>() but while not producing an error when I try to return the result (after specifing that the function returns a Map<<String,List<String>>>), when I run print(map.runtimeType); the output is CastMap<String, dynamic, String, List<String>> and not Map<<String,List<String>>>
Future<Map<String,List<String>>> getbreeds() async{
var json=await http.get('https://dog.ceo/api/breeds/list/all');
var linkedHashMap =Map.from(jsonDecode(json.body)) ;
var map=linkedHashMap['message'].cast<String,List<String>>();
print(map.runtimeType);
return map;
}
I expect the output of print(map.runtimeType); to be Map<<String,List<String>>> but instead, I get CastMap<String, dynamic, String, List<String>>
A CastMap<String, dynamic, String, List<String>> is a subtype of Map<String, List<String>> so that isn't a problem here. It's comparable to a situation where you specify a return type of num and the concrete value you return is an int.
The issue you're going to hit is that when you read a value from the map you'll have a List<dynamic> instead of a List<String>. You need to change the runtime types on the inner values as well.
var map = (linkedHashMap['message'] as Map).map((key, value) => MapEntry<String, List<String>>(key, List.from(value));