UseCallAdapter class
By annotating a method with @UseCallAdapter
, you can specify a custom adapter
class where you can adapt a call to another response wrapper
Usage
- Create the call adapter by extending CallAdapter:
pass in type parameters for the original call return type and adapted call return type.
Note: your adapter subclass must accept a single type parameter(T), where T is
the type of the unwrapped response from the original call. e.g.
"UserResponse" in "Future
"
class ResultCallAdapter<T> extends CallAdapter<Future<T>, Future<Result<T>>> {
@override
Future<Result<T>> adapt(Future<T> Function() call) async {
try {
final response = await call();
return Success<T>(response);
} catch (e) {
return Error(e);
}
}
}
- Set the adapter on an API method or the entire API interface:
- To apply the adapter to an individual method, use
@UseCallAdapter
on the method:
@UseCallAdapter(ResultCallAdapter)
Future<Result<UserResponse>> fetchData();
- To apply it to all methods in an Api interface, pass the adapter to
@RestApi
:
@RestApi(callAdapter: ResultCallAdapter)
abstract class MyApiService {
@GET('/data')
Future<Result<UserResponse>> fetchData();
}
Constructors
- UseCallAdapter(Type callAdapter)
-
const
Properties
- callAdapter → Type
-
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited