B
B
beduin012021-11-17 12:09:02
Dart
beduin01, 2021-11-17 12:09:02

Is it possible to initialize a controller via Get.put in main?

I am using GetX.

I want to do like this:

main() {
  Get.put(NavigationBarController());
  Get.put(JobListController());
  // ...
}


So that later inside the widget itself it was possible to use Get.find.

However, the following code complains about:
Error: "JobListController" not found. You need to call "Get.put(JobListController())" or "Get.lazyPut(()=>JobListController())"


At the same time, for some reason, such a feeling the first controller is initialized normally. if you often throw out the second code, then instead of an error, an interface is drawn.

The second code is:
class NavigationBarController extends GetxController {

  get to => Get.find<NavigationBarController>();

  var tabIndex = 0.obs;

  List<Widget> pages = [
      Dashboard(),
      TableStatisticSpeedView()
  ];

 Widget get currentPage => pages[tabIndex.value];

  void changeTabIndex(int index) {
    tabIndex.value = index;
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void dispose() {
    super.dispose();
  }
}


The code of the first on which the error:
class Dashboard extends StatelessWidget {
  var jobCtrl = Get.find<JobListController>();
  
  @override
  Widget build(context) {
    return Container(
      margin: EdgeInsets.only(top: 20.0),
      child: Column(children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
              onPressed: () => jobCtrl.startNewConfigToJob(), child: Text("Run")),


class JobListController extends GetxController {
  var _jobList = RxList<JobModel>();
  Rx<int> plannedJobConfigNumber = 0.obs;

  var _isLoading = true.obs;
  var _isEmpty = false.obs;
  var _isSuccess = false.obs;
  var _error = "".obs;

  get jobList => this._jobList;
  get isLoading => this._isLoading.value;
  get isEmpty => this._isEmpty.value;
  get isSuccess => this._isSuccess.value;
  get error => this._error.value;

  @override
  void onInit() {
    super.onInit();
    fetchJobStatus();
  }


  startNewConfigToJob()
  {
    try {
      DataProvider.startNewConfigToJob();
    } on AppException catch (e) {
       _error.value = e.message;
    }
  }

  configNumberChanged (String val) {
      if(val != '')  {
        plannedJobConfigNumber.value = int.tryParse(val) ?? 1;
        print(plannedJobConfigNumber);
      }
  }

  fetchJobStatus() async {
    print("fetchJobStatus");
    try {
      var data = await DataProvider.getJobsStatus(); 
      if (data.isEmpty) {
        _isLoading.value = false;
        _isEmpty.value = true;
        update();
      }
      else {
        _jobList.value = JobModel.listFromJson(data);
        _isLoading.value = false;
        _isSuccess.value = true;
        update();
      }
      
    } on AppException catch(e) {
      _isLoading.value = false;
      _error.value = e.message;
      // print("fetchJobStatus exception: $e");
    }

  }
  
  removeByPid(int pid) async {
    jobList.removeWhere((item) => item.jobPid == pid );
    await DataProvider.killByPid(pid);
    update();
  }


  
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Aleksandrovich, 2021-11-17
@beduin01

main() {
Get.put(JobListController());
Get.put(NavigationBarController());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question