0

Can you tell me how can I include the condition for Image.file on the following page? I would like to build it only when controller.image is not null.

I got an error: The following NoSuchMethodError was thrown building Container(padding: EdgeInsets.all(32.0)): The method '[]' was called on null. Receiver: null Tried calling:

when I first redirect to this page (and controller.image is null):

class HomePage extends GetView<HomeController> {


  final myController1 = TextEditingController();
  final myController2 = TextEditingController();


  @override
  Widget build(BuildContext context) {



    return Scaffold(
      appBar: AppBar(
        title: Text('Grobonet'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
            children: <Widget>[
              TextField(
                controller: myController1,
                decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText: 'Nazwisko'),

              ),
              TextField(
                controller: myController2,
                decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText: 'Miejscowosc'),

              ),
              IconButton(
                icon: Icon(
                  Icons.add,
                  size: 30,
                ),
                onPressed: () =>
                    Get.toNamed(
                        AppRoutes.PICK_IMAGE
                    ),
                color: Colors.pink,
              ),
              IconButton(
                icon: Icon(
                  Icons.save_outlined,
                  size: 30,
                ),
                /*onPressed: () =>
                    Get.toNamed(
                        AppRoutes.PICK_IMAGE
                    ),*/
                color: Colors.pink,
              ),


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

            ]
        ),
      ),

    );

  }
}

Controller:

class HomeController extends GetxController {

 final image = Get.arguments['image'];
 final file_loaded = Get.arguments['file_loaded'];

}

laxsore
  • 143
  • 1
  • 2
  • 9

2 Answers2

0

You can use collection-if. Just add if (controller.image != null) like so:

          if (controller?.image != null)
          Container(
            padding: EdgeInsets.all(32),
            child:  GetBuilder<HomeController>(
              builder: (_) {
                return Image.file(controller.image);
              },
            ),
          ),
ambiguous58
  • 1,241
  • 1
  • 9
  • 18
  • 1
    This can also throw error. `Null check on a null object`. In case controller is null. – Nikhil Badyal Apr 18 '21 at 09:39
  • I added Conditional member access to be safe. Thanks. – ambiguous58 Apr 18 '21 at 09:43
  • This still throws an exception: The following NoSuchMethodError was thrown building HomePage(dirty): The method '[]' was called on null. Receiver: null Tried calling: []("image") I added controller code for reference – laxsore Apr 18 '21 at 12:23
0

Use Get your arguments on the controller's onInit() method:

class HomeController extends GetxController {

   final File image;
   final File file_loaded;

   onInit(){
    image = Get.arguments['image'];
    file_loaded = Get.arguments['file_loaded'];
  }

}

In this way, you don't need to perform additional null checks except Get.arguments is actually null.

Update And you also need to update your view like this:

 Container(
        padding: EdgeInsets.all(32),
        child:  GetBuilder<HomeController>(
          builder: (_) {
            return Image.file(_.image); // not return Image.file(controller.image);
                                                // _ here is the instance of HomeController given by the GetBuilder
          },
        ),
      ),

Update 2 As you mentioned in the comments, you want to send data to a previously opened page (HomePage) from a second page (OCRDetailsPage). Then you don't need to pass arguments. You can get the HomeController instance in your OCRDetailsPage with Get.find() and set the variables and update the state like:

class OCRDetailsPage extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      final OCRDetailsController controller = Get.find();
      return Scaffold(
      appBar: AppBar(title: Text('OCR Details Page')),
      body: Center(
        child: FlatButton(
          color: Colors.blue,
          child: Icon(Icons.save_outlined),
          onPressed: () {
            final HomeController homeController = Get.find();
            homeController.ocr_text = controller.text;
            homeController.update();
            Get.toNamed(
              AppRoutes.HOME,
            );
           
          }),
       ),
     );
   }
 }
S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30
  • I did like following your guidance , but still got the same error. I'm not sure if you understand an issue, I have 2 pages: Home and OCR Details - start app with Home Page - proceed to OCR Details page (ocr image details displayed) - after pressing button on OCR Details I want to return ocr image details to Home page So first time when I launch the app I have null error because get.arguments is null – laxsore Apr 18 '21 at 17:21