۰ Ali
استفاده از dispose
جامعه فلاتر ایجاد شده در ۰۷ مرداد ۱۴۰۱

استاد حالا علاوه بر همین ویجتی که معرفی کردین ، طی دوره با controller‌های مختلفی آشنا شدیم هر کدام با کاربردهای مختلف. این که دقیقا تشخیص بدهیم چه زمانی بایستی از dispose استفاده کنیم ، رو توضیح میدین؟

طبق یک پاسخی که هست داریم :

if you are using a stream in your application then you have to release memory allocated to the stream controller. Otherwise, your app may get a warning from the PlayStore and AppStore about memory leakage.

سوالی که برای من پیش می‌آید این هست که در برخی controller‌ها ، همانند TextEditingController ، زمانی که به همین شیوه استفاده کنیم و از addListener بهره می‌بریم ، همان لحظه که تغییراتی در مقدار value ایجاد می‌شود دستورات خواسته شده ی ما هم اجرا می‌شود. به عبارتی شبیه به stream اما با استفاده از AddListener.

هم چنین در توضیحات addListener هم داریم :

If a listener is added twice, and is removed once during an iteration (e.g. in response to a notification), it will still be called again. If, on the other hand, it is removed as many times as it was registered, then it will no longer be called. This odd behavior is the result of the [ChangeNotifier] not being able to determine which listener is being removed, since they are identical, therefore it will conservatively still call all the listeners when it knows that any are still registered.
This surprising behavior can be unexpectedly observed when registering a listener on two separate objects which are both forwarding all registrations to a common upstream object.

و با این وجود متد dispose برای نمونه‌های ساخته شده از textEditingController‌ها هم وجود دارند.

398e-image.png

دقیقا من متوجه نمی‌شوم ایا استفاده زیاد این هم نهایتا منجر به memory leak خواهد شد یا نه و شفاف نشد که چه زمانی از dispose استفاده کنم یا استفاده نکنم یا این که بهتر است همیشه اگر برای هر controller ای در دسترس بود از آن استفاده کنم؟

و در مورد stream‌های دیگر در بحث پیاده سازی معماری bloc هم جای سوال بود که اینجا هم عملا وقتی ما می‌آمدیم از exampleBloc.stream.forEach استفاده می‌کردیم ، اینجا چرا به Memory leak و استفاده از dispose نیازی نشد.