5

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?

laxsore
  • 143
  • 1
  • 2
  • 9
  • 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. – djalmafreestyler Jul 22 '21 at 02:05

5 Answers5

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();
  }
}
Rohit Singh
  • 411
  • 3
  • 15
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

flakerimi
  • 2,580
  • 3
  • 29
  • 49
  • 1
    Thank you very much for help, but now I got this error: The getter 'image' was called on null. Receiver: null Tried calling: image – laxsore Apr 08 '21 at 20:59
  • probably not passing right, print(args), then see if it is object or list or null, then args['image'] or args.image – flakerimi Apr 09 '21 at 19:53
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'];
      }
      
   }

}
S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30
  • 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") – laxsore Apr 09 '21 at 09:32
  • I can see you are calling controller.image, you should call _.image – S. M. JAHANGIR Apr 09 '21 at 11:25
  • 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") – laxsore Apr 09 '21 at 14:36
  • Can you please include your Routes definition/GetPages? – S. M. JAHANGIR Apr 09 '21 at 15:24
  • Routes definition and GetPages added – laxsore Apr 09 '21 at 18:01
  • 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: https://github.com/smjxpro/flutter_stackoverflow/tree/getx_handle_get_arguments_in_controller – S. M. JAHANGIR Apr 10 '21 at 07:52
  • 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: https://github.com/ertiusmeo/OCR_GetX – laxsore Apr 10 '21 at 10:25
  • Now I can see what's your problem! Please check the updated answer! Hope it will fix the issue. – S. M. JAHANGIR Apr 10 '21 at 12:18
  • 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. – laxsore Apr 10 '21 at 16:54
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,
        );
Tharindu Welagedara
  • 2,660
  • 18
  • 27
0
 final args = Get.arguments ?? "";

check is there any arguments or not then check for

 final data = Get.arguments["data"] ?? "";