Search code examples
androidiosflutterflutter-getxstate-management

What's the difference between Get.to(page) and Get.to(() => page in Flutter GetX?


Basically why the framework says that Get.to(() => page) is better than the other method? For me they are the same but I'd like to know if there's any difference or impact.

I'm using the Get.to(() => page) but only because the framework says so.


Solution

  • There is a slight difference between both.

    1. Get.to(page) just uses default constructor to create new instance of the page. Meaning if your page has any dependencies, you would have to pass them as constructor arguments.

    2. Get.to(()=>page) creates page lazily using a closure or lambda function. As a result, you may build the page utilising GetX's dependencies by using the Get.put() or Get.lazyPut() methods.

    To summarize

    Get.to(MyPage()); // No dependency injection
    
    Get.to(() => MyPage()); // Allows for dependency injection