我有顫振應用程式,當我在另一個控制器中使用一個控制器時,我的應用程式中有很多控制器
所以有人建議我使用系結,但是當我使用 binging 并使用 get.put 方法時,它說我的控制器沒有初始化,誰能建議我如何在顫振中使用出價
uj5u.com熱心網友回復:
創建一個類并實作系結
class HomeBinding implements Bindings {}
您的 IDE 會自動要求您覆寫“依賴項”方法,您只需單擊燈,覆寫該方法,然后插入您將在該路線上使用的所有類:
class HomeBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut<HomeController>(() => HomeController());
Get.put<Service>(()=> Api());
}
}
class DetailsBinding implements Bindings {
@override
void dependencies() {
Get.lazyPut<DetailsController>(() => DetailsController());
}
}
現在您只需要通知您的路由,您將使用該系結在路由管理器、依賴項和狀態之間建立連接。
使用命名路由:
getPages: [
GetPage(
name: '/',
page: () => HomeView(),
binding: HomeBinding(),
),
GetPage(
name: '/details',
page: () => DetailsView(),
binding: DetailsBinding(),
),
];
使用正常路線:
Get.to(Home(), binding: HomeBinding());
Get.to(DetailsView(), binding: DetailsBinding())
在那里,您不必再擔心應用程式的記憶體管理,Get 會為您完成。
呼叫路由時會呼叫 Binding 類,您可以在 GetMaterialApp 中創建一個“initialBinding”以插入將創建的所有依賴項。
GetMaterialApp(
initialBinding: SampleBind(),
home: Home(),
);
系結生成器
創建系結的默認方法是創建一個實作系結的類。但或者,您可以使用 BindingsBuilder 回呼,以便您可以簡單地使用函式來實體化您想要的任何內容。
例子:
getPages: [
GetPage(
name: '/',
page: () => HomeView(),
binding: BindingsBuilder(() {
Get.lazyPut<ControllerX>(() => ControllerX());
Get.put<Service>(()=> Api());
}),
),
GetPage(
name: '/details',
page: () => DetailsView(),
binding: BindingsBuilder(() {
Get.lazyPut<DetailsController>(() => DetailsController());
}),
),
];
這樣您就可以避免為每條路由創建一個 Binding 類,從而使這更加簡單。
Both ways of doing work perfectly fine and we want you to use what most suit your tastes.
SmartManagement GetX by default disposes unused controllers from memory, even if a failure occurs and a widget that uses it is not properly disposed. This is what is called the full mode of dependency management. But if you want to change the way GetX controls the disposal of classes, you have SmartManagement class that you can set different behaviors.
How to change
If you want to change this config (which you usually don't need) this is the way:
void main () {
runApp(
GetMaterialApp(
smartManagement: SmartManagement.onlyBuilders //here
home: Home(),
)
)
}
SmartManagement.full It is the default one. Dispose classes that are not being used and were not set to be permanent. In the majority of the cases you will want to keep this config untouched. If you new to GetX then don't change this.
SmartManagement.onlyBuilders 使用此選項,只有在 init: 中啟動的控制器或使用 Get.lazyPut() 加載到系結中的控制器才會被釋放。
如果您使用 Get.put() 或 Get.putAsync() 或任何其他方法,SmartManagement 將無權排除此依賴項。
與 SmartManagement.onlyBuilders 不同,使用默認行為,即使使用“Get.put”實體化的小部件也會被洗掉。
SmartManagement.keepFactory 就像 SmartManagement.full 一樣,它會在不再使用時洗掉它的依賴項。但是,它將保留它們的工廠,這意味著如果您再次需要該實體,它將重新創建依賴項。
uj5u.com熱心網友回復:
例如,如果這是一個文本控制器,則需要在 init 狀態下這樣寫。
textController = TextEditingController();
如果您還沒有創建它,您應該在初始化狀態之前像這樣創建它。
late TextEditingController textController;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408825.html
標籤:
