SoFunction
Updated on 2025-04-10

Analysis of the use case of put and lazyPut functions in Flutter Getx

Case site

In the runApp function entry, a global controller is bound by lazy loading in the AppControllerBinding. In the A interface, you can get this controller through () and I exit the A interface (I haven't exited the APP yet). The controller called back the onClose function. I entered the product list interface again.

Tip me:

"AppController" not found. You need to call "(AppController())" or "(()=>AppController())"

At this time, I was very surprised. Since it is a global controller bound through the runApp entrance, why can it be obtained when the A interface is first used, and it is destroyed when it exits, and it is directly reported as an error when entering the A interface?

This requires a closer look at the two functions put and lazyPut in Getx and their parameters.

 <AppController>(AppController(),permanent: false);

The core is the second parameter permanent, default is false

When false, when put in view A, exiting view A will be destroyed. When B is in use, an error will be reported

"AppController" not found. You need to call "(AppController())" or "(()=>AppController())"

When true, once put, it will not be destroyed, and other views can also be used

<AppController>(() => AppController(),fenix: true);

The lazyPut function is different again, because the parameter has become fenix, which is not the permanent above, and the meaning of fenix is ​​also very different. It is easy to know that lazyPut is lazy to load. When that view is needed, it will be initialized in which view, and the fenix parameter is more important.

When fenix is ​​false:

Initialize when entering the view for the first time. When exiting the view, it will be destroyed. Entering the view again will cause an error.

"AppController" not found. You need to call "(AppController())" or "(()=>AppController())"

When fenix is ​​true:

Initialize when entering the view for the first time. When exiting the view, it is destroyed and enters the view again and is initialized again.
This is the reason for the crime scene! ! ! ! !

The above is the detailed content of the use case analysis of put and lazyPut functions in Flutter Getx. For more information about the put lazyPut function of Flutter Getx, please follow my other related articles!