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

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.

Related

why is this error 'The argument type 'List<AddEventModel>' can't be assigned to the parameter type 'AddEventModel''?

This maybe a silly questain i'm a beginner so help me. It is showing argument type List and a model 'AddEventModel' i create for Hive can't be assigned to parameter type 'AddEventModel' it is a valueNotifier.
Simply you can use this for loop to add all events to the events notifier
for(var event in allEvents){
eventsNotifier.value.add(event);
}

Why direct usage of generic value is not possible but same is possible if returned from method in Dart

Why is it assigning a value to a generic field is not possible when assigned directly, but same is possible when using a variable reference or method return value (here, same value is assigned to the variable and method returns the same value)?
class User {}
class Teacher extends User {}
class Student extends User {}
Future<User> getUser() {
return Future.value(Student());
}
void main() {
Future<Future<User>> fut = Future.value(getUser()); // <----- No error
Future<Future<User>> fut2 = Future.value(Future.value(Student())); // <----- Getting error
Future<User> userFut3 = Future.value(Student());
Future<Future<User>> fut3 = Future.value(userFut3); // <----- No error
}
Getting below error when Future.value(Future.value(Student())) assigned directly.
Error: The argument type 'Student' can't be assigned to the parameter type 'FutureOr<Future<User>>?'.
The issue here is that the parameter of Future<T>.value has the type FutureOr<T>. It can be either a future or a value.
Also, Dart type inference works by "pushing down" a context type, then trying to make the expression work at that type, and finally pushing the final static type back up.
If an expression like Future.value(...) has a context type, the missing type argument is always inferred from the context type.
When you write
Future<Future<User>> fut2 = Future.value(Future.value(Student()));
the context type of the outer Future.value, the type we know it should have, is Future<Future<User>>. That makes its argument have context type FutureOr<Future<User>>.
The argument is Future.value(Student()), where we don't yet know anything about Student() because we haven't gotten to it in the type inference yet, we're still working out way down towards it.
A Future<X> can satisfy that FutureOr<Future<User>> in two ways, either by being a Future<User> or by being a Future<Future<User>>.
Type inference then guesses that it's the latter. It's wrong, but it can't see that yet. The way type inference works, it has to use the context type when there is one, but the context type is ambiguous, and it ends up choosing the wrong option.
You are hitting an edge case of the type inference where the context type can be satisfied in two different ways, and the downwards type inference chooses the wrong one. It's a good heuristic that if you have a FutureOr<...> context type, and you see a Future constructor, you want the Future-part of the FutureOr<...>. It breaks down when you have FutureOr<Future<...>>. So, don't do that!
My recommendation, in complete generality, is to never have a Future<Future<anything>> in your program. Not only does it avoid problems like this, but it's also a better model for your code.
A future which eventually completes to something which eventually completes to a value ... just make it eventually complete to that value directly. Waiting for the intermediate future is just needless busywork.
Because in the function you defined the return type and dart knows the return type, but when assigning directly dart does not know Future.value(Student()) has a type of Future<User>. to fix this you have to tell the dart the type of the value, like this: Future.value((Future.value(Student())) as Future<User>);
this way dart will know the type of this value and treat it as a Future<User>.

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>.

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