Flutter getx controller get the current page context
Asked Answered
D

5

8

I would like to use context to show a custom dialog from cool alert in getxcontroller method. I have created the following controller

class HomePageController extends GetxController {
 
   @override
   void onInit() {
     super.onInit();
     getData();
   }

   void getData(){
    //perform http request here 
     //show cool alert 

     CoolAlert.show(
      context: context,  //here needs the build context
      type: CoolAlertType.success
      );
   }

}

Am using this controller in my stateless widget like

class HomePage extends StatelessWidget {
   HomePage({ Key? key }) : super(key: key);

   final _c = Get.find<HomePageController>();


    @override
    Widget build(BuildContext context) {
        return Container(
  
          );
    }
 }

How can i get the current homepage BuildContext in the controller inorder to show the cool alert.

Drub answered 15/10, 2021 at 4:28 Comment(3)
Initialize HomePageController on HomePage if you want access to the current screen contextBanausic
Have you found a solution?Abbess
// You can get the context here BuildContext? context = Get.context;Fearfully
B
4

You can simple use

Get.context

It will look like something like this

class HomePageController extends GetxController {
 
   @override
   void onInit() {
     super.onInit();
     getData();
   }

   void getData(){
    //perform http request here 
     //show cool alert 

     CoolAlert.show(
      context: Get.context,  //here needs the build context
      type: CoolAlertType.success
      );
   }

}
Bjork answered 31/3, 2023 at 13:49 Comment(0)
P
3

If you want to show a dialog or snackbar what need context as a required agument. You can use Get.dialog() and Get.snackbar, there function work same as showDialog and showSnackbar but *without* context or scaffod

Prank answered 4/11, 2021 at 1:58 Comment(0)
D
2

you can add the context to the construct function:

@override
Widget build(BuildContext context) {
  Get.put(HomePageController(context: context));
  return Container();
}

and for the HomePageController:
note: you need to wrap the function with Future.delayed(Duration.zero) otherwise it will throw an error

class HomePageController extends GetxController {
  late BuildContext context;

  HomePageController({required this.context});
  
   void getData(){
     Future.delayed(Duration.zero,(){
       CoolAlert.show(
         context: context,  //here needs the build context
         type: CoolAlertType.success
       );
     });
   }
  ...
}

Decoupage answered 22/5, 2022 at 2:32 Comment(0)
A
0

Recent Dart has null Value in order to compensate the null Value Use ! mark: So in this Case We're using this:

Get.context!

Aholla answered 20/9, 2023 at 11:59 Comment(0)
E
-2

You Need to Initialize the controller on the homepage like following

class HomePage extends StatelessWidget {
   HomePage({ Key? key }) : super(key: key);

   final _c = Get.put(HomePageController())..getData(context);


    @override
    Widget build(BuildContext context) {
        return Container(
  
          );
    }
 }

This will call getData Function and remove the onInit Function and Pass Buildcontext context parameter in getData Function.

Expiation answered 15/10, 2021 at 6:4 Comment(2)
How will i pass the context to the controller?Drub
@Drub I have changed my answer check that.Expiation

© 2022 - 2024 — McMap. All rights reserved.