How to handle Get.arguments as null in GetxController
Asked Answered
C

5

6

I have following GetX controller to pass parameters to page in Flutter:

UPDATED

class HomeController extends GetxController {

  File image;
  String ocr_text;

  onInit(){
    super.onInit();

    image = Get.arguments['image'];
    ocr_text = Get.arguments['ocr_text'];

    update();
  }

}

Binding:

class HomeBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<HomeController>(() => HomeController());
  }
}

I want to pass image from Ocr_details page:

FlatButton(
            color: Colors.blue,
            child: Icon(Icons.save_outlined),
              onPressed: () {
                Get.toNamed(
                  AppRoutes.HOME,
                  arguments: {'image': controller.image, 'ocr_text': controller.text},
                );
              }
          ),

to home page:

UPDATED:

Container(
                padding: EdgeInsets.all(32),
                child:  GetBuilder<HomeController>(
                  builder: (_) {
                    return _.image != null
                        ? Image.file(_.image)
                        : Container();
                  },
                ),
              ),

GetPages

class AppPages {
  static var list = [
    GetPage(
      name: AppRoutes.HOME,
      page: () => HomePage(),
      binding: HomeBinding(),
    ),
    GetPage(
      name: AppRoutes.PICK_IMAGE,
      page: () => PickImagePage(),
      binding: PickImageBinding(),
    ),
    GetPage(
      name: AppRoutes.OCR_DETAILS,
      page: () => OCRDetailsPage(),
      binding: OCRDetailsBinding(),
    )
  ];
}

Routes

class AppRoutes {
  static const String HOME = '/';
  static const String PICK_IMAGE = '/pick_image';
  static const String OCR_DETAILS = '/ocr_details';
}

But I'm getting following error: The following NoSuchMethodError was thrown building HomePage(dirty): The method '[]' was called on null. Receiver: null Tried calling:

I dont know if there is a way to check if argument is null and continue with rendering a widget?

Constructivism answered 8/4, 2021 at 19:28 Comment(1)
When you navigate to HOME, are you on an overlay widget like a Dialog or someting? because I had this error before when I tried to pass arguments to Get.arguments from overlay widgets. It should work fine if it's from a regular page.Commission
M
1

First, You are not using @override annotation with the onInit(). Second, You need to change the GetxController's onReady(). On ready will be called after the UI loads.

Instead of

class HomeController extends GetxController {

  File image;
  String ocr_text;

  onInit(){
    super.onInit();

    image = Get.arguments['image'];
    ocr_text = Get.arguments['ocr_text'];

    update();
  }
}

Try:

class HomeController extends GetxController {

  File image;
  String ocr_text;

  @override
  onReady(){
    super.onReady();

    image = Get.arguments['image'];
    ocr_text = Get.arguments['ocr_text'];

    update();
  }
}
Moersch answered 2/10, 2021 at 12:17 Comment(0)
I
0

On the page where do you want to receive,

final args = Get.arguments;


class HomeController extends GetxController {

  final args = Get.arguments;
 
}
Container(
                padding: EdgeInsets.all(32),
                child:  GetBuilder<HomeController>(
                  builder: (_) {
                    return args.image != null
                        ? Image.file(args.image)
                        : Container();
                  },
                ),
              ),],

then when you need it args.image or args.ocr_text

Infracostal answered 8/4, 2021 at 19:44 Comment(2)
Thank you very much for help, but now I got this error: The getter 'image' was called on null. Receiver: null Tried calling: imageConstructivism
probably not passing right, print(args), then see if it is object or list or null, then args['image'] or args.imageInfracostal
L
0

Instead of this:

class HomeController extends GetxController {

   final File image = Get.arguments['image'];
   final String ocr_text = Get.arguments['ocr_text'];

}

Use this:

class HomeController extends GetxController {

    File image;
    String ocr_text;

    onInit(){
      super.onInit();

      image = Get.arguments['image'];
      ocr_text = Get.arguments['ocr_text'];
      
      update();
   }

}

Update

I can see you are using controller.image. Instead of controller.image you need to use _.image inside your GetBuilder

Update 2 Just check if Get.arguments isn't null before trying to get the arguments and assigning.

class HomeController extends GetxController {

    File image;
    String ocr_text;

    onInit(){
      super.onInit();

      if(Get.arguments != null){
        image = Get.arguments['image'];
        ocr_text = Get.arguments['ocr_text'];
      }
      
   }

}
Leotie answered 9/4, 2021 at 7:38 Comment(9)
I got this error: The following NoSuchMethodError was thrown building Container(padding: EdgeInsets.all(32.0)): The method '[]' was called on null. Receiver: null Tried calling: []("image")Constructivism
I can see you are calling controller.image, you should call _.imageLeotie
I updated code as it is in first post, but still got his error: The method '[]' was called on null. Receiver: null Tried calling: []("image")Constructivism
Can you please include your Routes definition/GetPages?Leotie
Routes definition and GetPages addedConstructivism
I cannot reproduce the problem. I have created a new project and tried reproducing it. But in my case, it's working without any issues. Although I am using null-safe Flutter and not passing the 'image' file, I was able to send 'ocr_text' from 'OCRDetailsPage' to 'HomePage'. You can take a look at the reop: github.com/smjxpro/flutter_stackoverflow/tree/…Leotie
Thank you very much for help. I think in your case it works ok, because initilal page is OCR Details page, so ocr_text is not null when passing to home page. In my case initial page is Home page (without oct_text), than you are going to ocr details page and return value from there. You can see my code in here: github.com/ertiusmeo/OCR_GetXConstructivism
Now I can see what's your problem! Please check the updated answer! Hope it will fix the issue.Leotie
But onInit() will be called only when we initialize the widget (Home page), so now ocr_text and image won't be passed from OCR_Details page, because onInit wont be called again.Constructivism
R
0

This is a late answer, but If someone faces this issue in 2023 still when you are using GetPageRoute please pass the generateRoute(RouteSettings settings) param to the settings param in GetPageRoute. Ex:

return GetPageRoute<bool?>(
          settings: settings,
          page: () => YourScreen(),
          binding: YourBinding(),
          transition: Transition.downToUp,
        );
Robbierobbin answered 8/8, 2023 at 4:55 Comment(1)
This is still giving me null when getting the arguments I am passing. How to achieve this?Amber
L
0
 final args = Get.arguments ?? "";

check is there any arguments or not then check for

 final data = Get.arguments["data"] ?? "";
Landeros answered 8/8, 2023 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.