4

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 :(

CCP
  • 886
  • 1
  • 10
  • 30
  • why ? something's wrong with getx? im new know nothing what good or bad with state management – CCP Apr 08 '22 at 02:57

1 Answers1

8
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;
  }
}
Vinamra Jaiswal
  • 603
  • 7
  • 15
  • do i need to use the onClose too? – CCP Jan 13 '22 at 01:58
  • exactly not this. but find solution if controller any data build during setState or you call initState() function. then this variable or data you assign getX controller onInit() funciton. – AK IJ Jun 20 '23 at 04:11