How to solve: Flutter, GetX initState

Question

Asked by CCP on January 12, 2022 (source).

can someone explain how to use onInit in GetX and is it important to use the dispose/onClose ?

i want to GET api data and show it from the start of the app

already googled it didn't find anything helpful :(

Answer

Question answered by Vinamra J (source).

class ShoppingController extends GetxController {
  List<ProductModel> products = <ProductModel>[].obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getData();
  }

  Future<List<ProductModel>> getData() async {
    QuerySnapshot querySnapshot =
        await FirebaseFirestore.instance.collection('Products').get();
    products = querySnapshot.docs
        .map((m) => ProductModel.fromJson(m.data() as Map<String, dynamic>))
        .toList();
    return products;
  }
}
DART FLUTTER FLUTTER-GETX
SHARE: