String is not a subtype of int - flutter

I want to show suggestions in the search bar. But the highlighted portion of the image is not working i.e when the user inputs some query, it throws an error.
type 'String' is not a subtype of type 'int' of 'index'

Whatever your snapshot.data actually is, you are not handling it correctly.
I suggest you don't operate on raw maps (I guess you got them from a json webservice response?) but instead use model classes in your application, so that you have type safety and don't have to guess and get runtime errors when your guesses turned out to be wrong.
I don't know your models or your backend, so I can only link you this general guideline:
Flutter: Data and Backend - Serializing JSON inside model classes

String is not a subtype of int
This clearly states that, somewhere in your code, you had to pass an int but instead you are passing a String.
If my guess is right, one among this snapshot.data['shop_items']['productTitle'], is supposed to be an int, instead of a String.

Related

Is there any solution to make this code work

new Text(card_prod_qty),
Whenever i use this i get an error stated
type 'int' is not a subtype of type 'String'
Please help me solve it
I believe this Flutter convert int variable to string has the solution to your question,
basically you could just use card_prod_qty.toString()
The Text widget displays a string of text.
https://api.flutter.dev/flutter/widgets/Text-class.html
Considering the card_prod_qty variable is not a string, so you need to convert it.
card_prod_qty.toString()
So, just change
new Text(card_prod_qty),
to
new Text(card_prod_qty.toString()),

How to allow assignment of incompatible Typescript types?

I'm sharing type definitions between my server and front-end. They're defined in a separate npm package that both install. On the front-end, variables holding ObjectIds need to be typed as that but, on the client, I can assume they're always primitive strings.
I've got a number of places on the client that throw:
Type 'ObjectId' is not assignable to type 'string'.
What's the easiest way to deal with this error? Can I tell Typescript to accept string assignment to ObjectId and vice versa on the client? Should I be trying to override the Mongoose definition of ObjectId?
I'm considering an override of the sort
declare global {
export interface MyInterface1 {
variableWithObjectId1: string
}
export interface MyInterface2 {
variableWithObjectId2: string
}
}
I think this is supposed to work. It's the recommended solution for a similar issue but I have yet to make that work.
I'm hoping there is some way to globally transform ObjectId to string when the library is imported into the client.
ObjectId and String are not the same types at all, hence you cannot simply assign from one to the other.
You need to perform proper conversions.
For each of these errors on the client, I'm converting the type to a string like
var stringId = (myVariable as unknown) as string
I wish there was a cleaner way than this so I'm all ears if someone has a suggestion.

The name 'string' isn't a type and can't be used in an 'is' expression

During unit testing of a function returning different types of objects, I need to check if the type of returned object is the same as expected. Therefore, I need to pass multiple classes inside a variable. Then I need to use this variable with the is operator to check types.
final string = String;
assert('foo' is string);
But I am getting
error: The name 'string' isn't a type and can't be used in an 'is' expression.
I read somewhere that a library called Dart:mirrors can solve this problem but I haven't seen an actual example.
In unit testing, you know the expected answer. There shouldn't be a need to make your types variables.
Instead, just assert with the strong typed
assert('foo' is String);
I found the answer. The trick to create an instance of the type that I want to assert, then use runtimeType property.
If a class is called User from a.dart and another one is also called User from b.dart, runtimeType won't be the same
final string = 'anything'.runtimeType;
assert('foo'.runtimeType is string);

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

Serializing data using IEnumerable<T> with WebGet

possible duplicate:
Cannot serialize parameter of type ‘System.Linq.Enumerable… ’ when using WCF, LINQ, JSON
Hi,
If my method signiature looks like this, it works fine.
[WebGet]
MyClass[] WebMethod()
If the signiature looks like this
[WebGet]
IEnumerable<T> WebMethod()
I get the following error:
Cannot serialize parameter of type 'X.Y.Z.T+<WebMethod>d__2c' (for operation 'WebMethod', contract 'IService') because it is not the exact type 'System.Collections.Generic.IEnumerable`1[X.Y.Z.T]' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute.
I have tried adding.
ServiceKnownType(typeof(IEnumerable))
Same error.
Is this a bug in 2010 beta 2, or is this likely to be correct going forward?
Thanks
The iterator types generated by the C# compiler are not serializable and never will be.
If you read this page, you'll see that it wouldn't make sense to serialize the iterator.
You need to return an array.
EDIT: The simplest way to do that is to move your iterator to a seperate method, and change WebMethod to
[WebGet]
MyClass[] WebMethod() { return OtherMethod().ToArray(); }
I've run into the same issue, and in my case it's simply not possible to change my entire object graph from iterator-based IEnumerable to concrete types. I simply cannot afford the memory to convert over to concrete types like List or Array. Additionally, what about the case where I return an IEnumerable of some object that has an IEnumerable property. It is unacceptable that I have to recurse my entire object graph converting all IEnumerables.
I don't see any good reason why the DataContractSerializer can't iterate any IEnumerable type and render its elements to XML in the same manner as any other collection type, even if the IEnumerable doesn't have a concrete backing type.
This is a bug which should be fixed.