attiya sheikh
attiya sheikh

Reputation: 1

Flutter: Initializing variables on startup with getx data

I am trying to use the values saved in the getx controller to initialize several variables and list in my app. In Flutter, getx is asynchronous so it results in the variables and list initializing later on in the code which is creating problems with my app as some of the variables are null when the build method is called. I also tried async await, timers, addPostCallBack and Future.delayed but they also gives error on screen load and after some delay they show list populated with data. Is there any way to initialize the list or variables after Get.put(Controller with arguments) that results in non-empty variable or list is flutter?

Here is the code:

    @override
  void initState(){
 

    teacherSubjectClassController = Get.put(TeacherSubjectClassController(widget.teacherId,widget.subjectId),tag: 'attachments $classSubjectCount');
    print('controller: $teacherSubjectClassController');


    super.initState();

    // setState((){
    //   _choicesList = teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    // });
    // _choicesList = teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    // chipsList= getChoiceChips(teacherSubjectClassController);
    defaultChoiceIndex = 0;
    defaultTypeIndex = 0;
     // _choicesList = teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    print('choice $_choicesList');

    // getChoiceChips();
    // List _choicesList = teacherSubjectClassController.getTeacherClassSubject(widget.teacherId,widget.subjectId).value.map((section) => section.className+" "+section.sectionName).toList();
    // _choicesList=List.filled(teacherSubjectClassController.teacherSubjectClass.value.length, 0,growable: true);
    // print('choice $_choicesList');

    // WidgetsBinding. instance. addPostFrameCallback((_) => getTimer());
    // getChoiceChips();
    // print('subject ${widget.subjectName}');
    // teacherSubjectClassController.getTeacherClassSubject(widget.teacherId, widget.subjectId);

    // Future.delayed(Duration(milliseconds: 500),()async{
    //   _choicesList = await teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    //   setState(() {});
    //   print("chList: $_choicesList");
    //
    //     // classId=await teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.classId).toList();
    //     // print("classId: $classId");
    //     //
    //     // sectionsId=await teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.secId).toList();
    //     // print("sectionId: $sectionsId");
    //
    // });

    // (() async {
    //   getTimer();
    //   // _choicesList = await teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    //   print("chList: $_choicesList");
    //
    // })();
  }

    getChoiceChips(BuildContext context)  {

    // await Future.delayed(Duration.zero); // <-- Add a 0 dummy waiting time
    _choicesList =  teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();

    classId= teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.classId).toList();
    print("classId: $classId");

    sectionsId= teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.secId).toList();
    print("sectionId: $sectionsId");

    setState((){
    });

    showClassChips = true;
    int idx = _choicesList[defaultChoiceIndex].indexOf(" ");
    parts = [_choicesList[defaultChoiceIndex].substring(0,idx).trim(), _choicesList[defaultChoiceIndex].substring(idx+1).trim()];
    print("parts ${parts}");

    teacherClassSecSubHwController =  Get.put(TeacherClassSecSubHwController(teacherId: widget.teacherId,subjectId: widget.subjectId,sectionId: sectionsId[defaultChoiceIndex],classId: classId[defaultChoiceIndex], type: typeList[defaultTypeIndex]),tag: 'attachments $count');
    print("hwController: $teacherClassSecSubHwController");

  }

    getTimer() async {
      print('Started at ${DateTime.now()}');
      _choicesList = await Future.delayed(Duration(milliseconds: 350)).then((value) => teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList());
      print('Awaited list is $_choicesList');
      return _choicesList;
    }


  @override
  Widget build(BuildContext context) {
    
// WidgetsBinding.instance.addPostFrameCallback((_) => getChoiceChips(context));

    // teacherSubjectClassController.teacherSubjectClass.refresh();


    getChoiceChips(context);
    // _choicesList = teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    print("chList: $_choicesList");

    // _choicesList =  Get.put(TeacherSubjectClassController(widget.teacherId,widget.subjectId),tag: 'attachments ${++classSubjectCount}').teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    // print("chList: $_choicesList");
    // Future.delayed(Duration(milliseconds: 500),()async{
    //   _choicesList = await teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    //   setState(() {});
    //   print("chList: $_choicesList");

      // classId=await teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.classId).toList();
      // print("classId: $classId");
      //
      // sectionsId=await teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.secId).toList();
      // print("sectionId: $sectionsId");

    // });

    // var button = IconButton(onPressed: () async {
    //   // _choicesList = await teacherSubjectClassController.teacherSubjectClass.value.map((section) => section.className+" "+section.sectionName).toList();
    //   // print("chList: $_choicesList");
    //
    //
    //
    //   // classId=await teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.classId).toList();
    //   // print("classId: $classId");
    //   //
    //   // sectionsId=await teacherSubjectClassController.teacherSubjectClass.value.map((classes) => classes.secId).toList();
    //   // print("sectionId: $sectionsId");
    //
    //   setState((){
    //     // getTimer();
    //   });
    //   // showClassChips = true;
    //   // int idx = _choicesList[defaultChoiceIndex].indexOf(" ");
    //   // parts = [_choicesList[defaultChoiceIndex].substring(0,idx).trim(), _choicesList[defaultChoiceIndex].substring(idx+1).trim()];
    //   // print("parts ${parts}");
    //
    //
    //
    // }, icon: Icon(Icons.arrow_downward));

    // button.onPressed?.call();

    // teacherClassSecSubHwController = Get.put(TeacherClassSecSubHwController(teacherId: widget.teacherId,subjectId: widget.subjectId,sectionId: sectionsId[defaultChoiceIndex],classId: classId[defaultChoiceIndex], type: typeList[defaultTypeIndex]));
    // print("hwController: $teacherClassSecSubHwController");

  print("selectedClassId ${classId[defaultChoiceIndex]}");
    print("selectedSectionId ${sectionsId[defaultChoiceIndex]}");

In this code, initially _choicesList is null which creates problem in getting data.

I have tried delay functions and timer but they are initialized again and again which affects performance of app.

Upvotes: 0

Views: 1191

Answers (1)

yaman hafez
yaman hafez

Reputation: 48

Hello My Friend You Can Make Loading Screen This Best True Way;

// When You Call Api Put This
bool isLoading = false;
getData()async{
isLoading = true;
update();
await //Your Or Post Function getFunction
isLoading = false;
update();
}

This Is Controller

And Your View

GetBuilder<YourController>(
builder(controller){
return controller.isLoading == true 
? LoadingWidget() 
: YourPage();
}
),

I Hope It Help You

Upvotes: 0

Related Questions