Skip to content

Instantly share code, notes, and snippets.

@Kaival-Patel
Created May 21, 2023 14:53
Show Gist options
  • Select an option

  • Save Kaival-Patel/05ef3e7be9da0dd9c3e548d096ff2157 to your computer and use it in GitHub Desktop.

Select an option

Save Kaival-Patel/05ef3e7be9da0dd9c3e548d096ff2157 to your computer and use it in GitHub Desktop.
Getx Controller for fetching the users listing
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