如何在 GetxController 中将 Get.arguments 处理为 null

lax*_*ore 6 dart flutter flutter-getx

我有以下 GetX 控制器将参数传递到 Flutter 中的页面:

更新

class HomeController extends GetxController {

  File image;
  String ocr_text;

  onInit(){
    super.onInit();

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

    update();
  }

}
Run Code Online (Sandbox Code Playgroud)

捆绑:

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

我想从 Ocr_details 页面传递图像:

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

Run Code Online (Sandbox Code Playgroud)

到主页:

更新:

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

获取页面

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(),
    )
  ];
}
Run Code Online (Sandbox Code Playgroud)

路线

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

Run Code Online (Sandbox Code Playgroud)

但我收到以下错误: 构建主页(脏)时抛出以下 NoSuchMethodError:在 null 上调用方法“[]”。接收者: null 尝试调用:

我不知道是否有办法检查参数是否为空并继续渲染小部件?

fla*_*imi 0

在您要接收的页面上,

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();
                  },
                ),
              ),],
Run Code Online (Sandbox Code Playgroud)

然后当你需要它 args.imageargs.ocr_text