Reputation: 372
Future dioGet<T extends BaseModel>(String path,T model)async{
...asdsasa
}
abstract class BaseModel<T>{
Map<String,dynamic> toJson();
T fromJson(Map<String,Object> json);
}
In the dioGet function above, (T extends BaseModel).What does this do?
does the function return a BaseModel object?
or, the object that will come to the function needs to be extends from BaseModel?
What is T?
Upvotes: 0
Views: 483
Reputation: 5333
First of all, the T
inside Future dioGet<T extends BaseModel>(String path, T model)
defines a generic type (hence the common short form of that is T
) of a model
parameter you will pass to the function. This is the official explanation from the Dart language tour:
If you look at the API documentation for the basic array type, List, you’ll see that the type is actually
List<E>
. The <…> notation marks List as a generic (or parameterized) type—a type that has formal type parameters. By convention, most type variables have single-letter names, such as E, T, S, K, and V.
Now, let's move to the part of what the T extends BaseModel
means. By using extend for generic types, you can restrict them and, for instance, only allow passing types that extend/implement a specific type:
When implementing a generic type, you might want to limit the types that can be provided as arguments, so that the argument must be a subtype of a particular type. You can do this using extends.
In the example you provided, only types that extend the BaseModel
are allowed to use as a model
parameter type in the dioGet
function.
Now, to answer your question, it's the second case - the object that comes to the function must extend BaseModel
.
Upvotes: 1