How to handle Get.arguments as null in GetxController

Viewed 8371

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?

3 Answers

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();
  }
}

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

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'];
      }
      
   }

}
Related