flutterdartflutter-getx

Flutter Getx route argument data is lost when hot reload


Question might be misleading, but I'm currently working on a project where I use Get.toNamed a lot and I'm sending some data every time. But, when I hot reload the changes I get

════════ Exception caught by widgets library ═══════════════════════════════════
type 'Null' is not a subtype of type `int` 

and I'm changing route with

onTap: () {
    print(_sliderValue);
    Get.toNamed('/stations/detail/booking', arguments: {
        'duration': _sliderValue,
    });
},

Edit:

As I have mentioned, I'm using Stateless Widget to for that screen, so the code I have is:

class ConnectorScreen extends StatelessWidget {
  const ConnectorScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final Map<String, dynamic> arguments =
        Get.arguments as Map<String, dynamic>;
    final Connector connector = arguments['connectors'];

which gives error that type 'Null' is not a subtype of type Connector once I hot reload.

Is there any proper way of not losing data while routing between pages or hot reload?


Solution

  • Im always using Get.toNamed to send arguments but never found issue like this soo maybe you can try this

    You can route to page by SecondPage.show(somethingId : 'id#0');

    class SecondPage extends StatelessWidget {
      static String get name => '/second-page';
    
      const SecondPage({Key? key}) : super(key: key);
    
      static Future<void> show({
        required String somethingId,
      }) async {
        await Get.toNamed(
          name,
          arguments: {
            'somethingId': somethingId,
          },
        );
      }
    
      @override
      Widget build(BuildContext context) {
       ....
      }
    

    and in controller

    class SecondPageController extends GetxController {
      @override
      void onInit() async {
        somethingId.value = Get.arguments['somethingId'] ?? '';
    
        super.onInit();
      }
    
      final somethingId = ''.obs;
    }