2

I want to bind a controller to view 1 but I don't want to go to that view 1 via using Get.to(Page());.
Instead I want to use view 1 directly inside view 2 by creating an object.

Simplified code (BTW I'm using Veiw1Controller variables inside View1 itself)

class Veiw2 extends GetView<Veiw2Controller>{
     return View1();
}

When I'm doing the above code, it throws an error saying

"View1Controller" not found. You need to call "Get.put(View1Controller())" or "Get.lazyPut(()=>View1Controller())"

I did call Get.put(...) in the binding but I think since we are not calling Get.to()therefore GetX does not realize when we are using that view and it does not bind the dependencies
Here is what I've done

class View1 extends Bindings {
  @override
  void dependencies() {
    Get.put<View1Controller>(
      View1Controller(),
    );
  }
}

What is the best way to do that?

Yaya
  • 4,402
  • 4
  • 19
  • 43

1 Answers1

3

well, the Bindings API was made to work together with the Getx navigation features, without actually Get.to(), Get.toNamed()..., you should not expect the automatic dependency injection of Getx.

However, you still could inject those dependencies manually: like this:

class BindingsOne extends Bindings {
  @override
  void dependencies() {
    Get.put<View1Controller>(
      View1Controller(),
    );
  }
}

Now when you want to get a Widget:

Widget getViewWidget() {
  BindingsOne().dependencies(); // this will inject all your dependencies from the bindings.
  return YourWidget();
}

you could also inject them before you use Navigator routing features.

 // ...
  BindingsOne().dependencies(); // this will inject all your dependencies from the bindings.
  Navigator.of(context).push(MaterialPageRoute(builder: (context) => YourWidget()));
}
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35