Answer the question
In order to leave comments, you need to log in
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());
// ...
}
Error: "JobListController" not found. You need to call "Get.put(JobListController())" or "Get.lazyPut(()=>JobListController())"
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();
}
}
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
main() {
Get.put(JobListController());
Get.put(NavigationBarController());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question