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

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?

Related

DART: attribute of attribute of class

I have a classmodel in dart with a json mapper like
class DA_Field {
final String strGROUP;
DA_Field({
required this.strGROUP, });
static DA_Field fromJson(json) => DA_Field(
strGROUP: json['GROUP'] as String, );
}
and I want to set a attribute of the attribute strGROUP via a function - so that I can call that sub-attribute 'width' for example by:
intWidth = DA_Field.strGROUP.width
My first attempt was to create a setter - but after a lot of google, I only get in touch to create a setter for the whole modelclass, not for all of the single attributes.
The values should be calculated by a function outside of the class - I thought maybe this could be possible by .forEach function - like ?
DA_Field.forEach((k,v) => k.width = functionxx(k));
But i get following errors:
error: The setter 'width' isn't defined for the type 'DA_Field'. (undefined_setter...)
error: The argument type 'void Function(DA_Field, dynamic)' can't be assigned to the parameter type 'void Function(DA_Field)'. (argument_type_not_assignable)
Can anyone give me a suggestion on how to do that?
Thanks..

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

Flutter: add string to List with .addAll()

I have to add value that I receive from the api server.
print(userInfo['ID']['List'][0].runtimeType); // returns String
(userInfo['ID']['List'] as List).addAll(result['id_num']);
However, I am receiving I/flutter (23015): type 'String' is not a subtype of type 'Iterable<dynamic>'
How can I add value to the List in this case? Result['id_num'] also returns the type of String.
Using addAll:
addAll accepts an Iterable but you're giving it a String. All you need to do is create an Iterable from the String like this:
(userInfo['ID']['List'] as List).addAll([result['id_num']]);
Using add:
If you don't wish to use addAll, you can directly add String using add method:
(userInfo['ID']['List'] as List).add(result['id_num']);

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!

How to cast Object to a specified type in Flutter

I get a linter error but don't know how to fix it
final FoodScreenArguments args = ModalRoute.of(context).settings.arguments;
A value of type Object can't be assigned to a variable of type FoodScreenArguments.
Try changing the type of the variable, or casting the right-hand type to FoodScreenArguments .
Easiest way :
final args = ModalRoute.of(context).settings.arguments as FoodScreenArguments ;
Unlike java's parentheses casting (), in flutter, it uses as keyword. Here is an example from my code where I am printing a variable of class.
debugPrint("rht: List size ${((albumList1.first) as AlbumData).title}");