Is there any solution to make this code work - flutter

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

Related

Dart null variable creation

I just started learning Dart. There is some problem when I am creating a null variable. When I type
String someVar;
it throws an error but when i type
dynamic someVar;
it doesn't. I tried doing alternative methods mentioned in Dart's doc but even those methods do not seem to work until i have a dynamic var type. Can anyone tell me what is it?
Dart has a feature called null safety, so when you define something which might have a null value, you have to use ?.
In your code above, try something like String? somevar;
If you are going to initialize the variable later, then define late String somevar;
If you have null safety on String someVar; will give you an error because it cannot be null.
If u want a variable to be a nullable string, use String? someVar.
Try avoiding the use of dynamic because it will allow for the type to not be fixed. This can potentially cause the accidental assigning of a value with a type you are not expecting.

type 'Future<void>' is not a subtype of type 'Widget'

I have looked for similar answers and tried using their proposed solutions with no success. I am trying to build a dropdown with data from the dynamic. Every time I try to run it, it gives me a
"type 'Future' is not a subtype of type 'Widget'" and I am using Getx framework in flutter
If you had code then it would easy to pinpoint the issue. Basically you are returning nothing from your function that is its return type is Future and calling that function for some parameter that expects to receive a widget. So your function should return some class that extends statelessWidget or statefulWidget.
Hope makes sense.

String is not a subtype of int

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.

What's the difference between List<Type/*1*/>> and List<Type/*2*/>>

Hi I was making a Todo App in flutter but got stuck thanks to an error in in my code:
`Error: The argument type 'List<Todo/1/>' can't be assigned to the parameter type 'List<Todo/2/>'.
'List' is from 'dart:core'.
'Todo/1/' is from 'package:Database/main.dart' ('lib/main.dart').
'Todo/2/' is from 'package:Database/model/Todo.dart' ('lib/model/Todo.dart').
? new TodoList(todo: todoList)`
All types are of List but it's telling me that values can't be assigned. I've tried casting but doesn't seem to fix the issue.
So my question is What's the difference between the List types. if I know I can find a fix myself(I'm trying to rely on my nkowledge of things to fix errors. it's a challenge for myself).
It seems like you have two different classes with the same name.
Even with the same class name, those classes are different. You can map your first List to the type List by using the map function:
list.map( (todo1) => Todo2() ).toList()
you can import Todo class like import 'package:Database/model/Todo.dart' as model and have your list like this: List<model.Todo>.

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