[GetX機能整理]4.依存性注入


GetXの依存注入と依存注入を理解した.
バインドを使用してインスタンス化する方法は4つあります.

Dependency Controller

class DependancyController extends GetxController {

  @override
  Widget build(BuildContext context) {

依存性管理ページ

class DependancyManagePage extends StatelessWidget {
  const DependancyManagePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Column(
        children: [
Get putメソッドの使用
          MaterialButton(
              child:Text("Get Put 방식"),
              onPressed: (){
              	Get.to(
                	GetPut(),
                    //필요한 의존성 주입을 어떻게 할 건지 정의를 내림
                    binding:BindingBuilder((){
                    	Get.put(DependancyController());
                    	})
                    );
              }
              ),
  • バインディングは、ルーティングフェーズでインスタンス管理を行うことができる.ページ作成時にコントローラを作成し、ページが消えてもコントローラを閉じます.
    =>メモリ消去はパフォーマンスを低下させません.
  • Get.LazyPutの利用
    MaterialButton(
                  child:Text("Get Lazy Put 방식"),
                  onPressed: (){
                    Get.to(
                          GetLazyPut(),
                          binding:BindingBuilder((){
                              Get.lazyPut<DependancyController>(
                              () => DependancyController());
                              }),
                          );
                        }
              ),
  • LazyPutはビルダーを使用します.Putとは異なり、インスタンス化されていないため、メモリにアップロードされません.アクセスするとメモリに入ります.
  • Get.putAsyncの使用
              MaterialButton(
                  child:Text("Get Put Async 방식"),
                  onPressed: (){
                    Get.to(
                      Getput(),
                      binding: BindingsBuilder((){
                        Get.putAsync<DependancyController>(() async {
                        await Future.delayed(Duration(seconds:5));
                        //5초 후 인스턴스화 됨.
                        return DependancyController();} )
                      })
                    );
                  }
              ),
  • Get putと似ていますが、ページアクセス時にデータを非同期で受信したり、加工後にコントローラをインスタンス化したりすることができます.
  • Get Createの使用
    MaterialButton(
                  child:Text("Get Create 방식"),
                  onPressed: (){
                  Get.to(
                  	Getput(),
                    binding: BindingsBuilder(() {
                      Get.created<DependancyController>(
                      () => DependancyController());
                      }),
                      );
                  }
              ),
            ],
          )
        )
      }
    }
  • の最初の3つの方法は、単一の色調(作成され、どこでも共有されます)です.逆に、Get Creatは作成を続行し、複数のインスタンスが存在します.自動的に削除されないため、使用終了のたびに削除する必要があります.
  • Get Putページ

    class GetPut extends StatelessWidget {
      const GetPut({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body:Column(

    Get LazyPutページ

    class GetLazyPut extends StatelessWidget {
      const GetLazyPut({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body:Column(
    ソース:https://www.youtube.com/watch?v=KDTjo-6jFKs