Created
May 21, 2023 14:53
-
-
Save Kaival-Patel/05ef3e7be9da0dd9c3e548d096ff2157 to your computer and use it in GitHub Desktop.
Getx Controller for fetching the users listing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:get/get.dart'; | |
| import '/src/backend/api/user_nw_service.dart'; | |
| import '/src/models/user_model.dart'; | |
| class UserListController extends GetxController | |
| with StateMixin<List<UserModel>> { | |
| @override | |
| void onInit() { | |
| super.onInit(); | |
| fetchUsers(); | |
| } | |
| fetchUsers() async { | |
| change([], status: RxStatus.loading()); | |
| try { | |
| var res = await UserNetworkService().getListOfUsers(); | |
| if (res.results!.isNotEmpty) { | |
| change(res.results!, status: RxStatus.success()); | |
| } else { | |
| change([], status: RxStatus.empty()); | |
| } | |
| } on Exception catch (err) { | |
| change([], status: RxStatus.error(err.toString())); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment