i want make get request after the first one finished because i need to send data from the response of first request with the body of second request to the server .. how i do this with getx
thanks
class ProductsController extends GetxController with StateMixin<Posts> {
@override
void onInit() {
getData();
super.onInit();
}
getData() async {
try {
change(null, status: RxStatus.loading());
await postsApiProvider
.getPosts()
.then((value) {
change(value, status: RxStatus.success());
});
} catch (exception) {
change(null, status: RxStatus.error(exception.toString()));
}
}
// i want this function fire after getData()
_getRelated() async {
try {
await postsApiProvider
.getRelated(
price: value.region ----> because i need access to getDate values
)
.then((value) {
});
} catch (e) {
debugPrint(e.toString());
}
}
}
I tried that method but it didn’t work :
@override
void onReady() {
_getRelated();
super.onReady();
}
2
Answers
try using try with finally e.g
You can achieve your result by using
finally
. But I would suggest creating a function for that, so that in future if you want to add one more function based on second function’s callback then you can easily achieve it.Here’s the example:
then you can call the global function in the
onInit
method:By using this way, you can add more functions in the future and your code will look cleaner than before.