I have two pages: HomePage and DetailsPage and associated GetxControllers.
HomePage:
class HomePage extends GetView<HomeController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('HomePage')),
body: Container(
child: Obx(
() => ListView.builder(
itemCount: controller.task.length,
itemBuilder: (context, index) {
return ListTile(
leading: Text('${index + 1}'),
title: Text(controller.task[index]["name"]),
onTap: () {
Get.to(
DetailsPage(),
arguments: controller.task[index]["name"],
);
},
);
},
),
),
),
);
}
}
HomeController:
class HomeController extends GetxController {
final TaskRepository repository;
HomeController({@required this.repository}) : assert(repository != null);
final _task = [].obs;
set task(value) => this._task.assignAll(value);
get task => this._task;
onInit() {
super.onInit();
getAllTask();
}
getAllTask() {
repository.getAll().then((value) => task = value);
}
}
As you can see the HomeController depends on a TaskRepository which is a mock repo.
And my DetailsPage:
class DetailsPage extends GetView<DetailsController> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
GestureDetector(
onTap: () {
Get.back();
},
child: Row(
children: [
Icon(Icons.arrow_back),
Text('Go Back'),
],
),
),
Expanded(
child: Center(
child: Obx(
() => Text(controller.taskDetail.value),
),
),
),
],
),
);
}
}
DetailsController:
class DetailsController extends GetxController {
final taskDetail = ''.obs;
@override
void onInit() {
super.onInit();
taskDetail.value = Get.arguments;
}
}
I have created an AppDependencies class to initialize the dependencies (controllers, repositories, API clients, etc.):
class AppDependencies {
static Future<void> init() async {
Get.lazyPut(() => HomeController(repository: Get.find()));
Get.lazyPut(() => DetailsController());
Get.lazyPut(() => TaskRepository(apiClient: Get.find()));
Get.lazyPut(() => TaskClient());
}
}
I am initializing all the dependencies by calling AppDependencies.init()
on main()
:
void main() async {
await AppDependencies.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
Going back to HomePage and then Going again to DetailsPage
As you can see on the third image, going back from DetailsPage to HomePage and going back to DetailsPage causes an exception saying:
"DetailsController" not found. You need to call "Get.put(DetailsController())" or "Get.lazyPut(()=>DetailsController())"
But I already did that on main()
. I also tried with Get.put()
instead of Get.lazyPut()
but I found that for Get.put()
any dependencies of any other dependency must be registered before the dependent one. For example, HomeController depends on TaskRepository so TaskRepository must be before HomeController if using Get.put()
like:
Get.put(TaskRepository());
Get.put(HomeController());
And this is not what I want because I don't wanna track what comes before what manually. And I found that this causes if there's a back button (which almost every page has).
What I am doing wrong here?