11import 'package:flutter/foundation.dart' ;
2+ import 'package:load_switch/src/load_switch_types.dart' ;
23
34/// Controller for managing the state of a [LoadSwitch] widget.
45///
@@ -17,6 +18,7 @@ class LoadSwitchController extends ChangeNotifier {
1718 bool _value;
1819 bool _isLoading;
1920 bool _isActive;
21+ bool _isDisposed = false ;
2022
2123 /// The current value of the switch (on/off).
2224 bool get value => _value;
@@ -29,6 +31,9 @@ class LoadSwitchController extends ChangeNotifier {
2931
3032 /// Sets the value of the switch and notifies listeners.
3133 set value (bool newValue) {
34+ if (_isDisposed) {
35+ return ;
36+ }
3237 if (_value != newValue) {
3338 _value = newValue;
3439 notifyListeners ();
@@ -37,6 +42,9 @@ class LoadSwitchController extends ChangeNotifier {
3742
3843 /// Sets the loading state of the switch and notifies listeners.
3944 set isLoading (bool loading) {
45+ if (_isDisposed) {
46+ return ;
47+ }
4048 if (_isLoading != loading) {
4149 _isLoading = loading;
4250 notifyListeners ();
@@ -45,6 +53,9 @@ class LoadSwitchController extends ChangeNotifier {
4553
4654 /// Sets the active state of the switch and notifies listeners.
4755 set isActive (bool active) {
56+ if (_isDisposed) {
57+ return ;
58+ }
4859 if (_isActive != active) {
4960 _isActive = active;
5061 notifyListeners ();
@@ -53,6 +64,9 @@ class LoadSwitchController extends ChangeNotifier {
5364
5465 /// Toggles the value of the switch if it's not loading and is active.
5566 void toggle () {
67+ if (_isDisposed) {
68+ return ;
69+ }
5670 if (! _isLoading && _isActive) {
5771 value = ! value;
5872 }
@@ -64,24 +78,43 @@ class LoadSwitchController extends ChangeNotifier {
6478 /// the future completes or throws an error. Updates the value of the switch
6579 /// based on the result of the future.
6680 ///
67- /// If [onChange ] is provided, it will be called with the new value.
81+ /// If [onChanged ] is provided, it will be called with the new value.
6882 /// If [onError] is provided, it will be called with any error that occurs.
6983 Future <void > executeWithLoading (
70- Future < bool > Function () future , {
71- Function ( bool ) ? onChange ,
72- Function ( Object ) ? onError,
84+ LoadSwitchToggleCallback onToggle , {
85+ ValueChanged < bool > ? onChanged ,
86+ LoadSwitchErrorCallback ? onError,
7387 }) async {
74- if (_isLoading || ! _isActive) return ;
88+ if (_isDisposed || _isLoading || ! _isActive) {
89+ return ;
90+ }
7591
7692 isLoading = true ;
7793
7894 try {
79- value = await future.call ();
80- onChange? .call (value);
81- } catch (error) {
82- onError? .call (error);
95+ value = await onToggle ();
96+ if (_isDisposed) {
97+ return ;
98+ }
99+ onChanged? .call (value);
100+ } catch (error, stackTrace) {
101+ if (_isDisposed) {
102+ return ;
103+ }
104+ onError? .call (error, stackTrace);
83105 } finally {
84- isLoading = false ;
106+ if (! _isDisposed) {
107+ isLoading = false ;
108+ }
109+ }
110+ }
111+
112+ @override
113+ void dispose () {
114+ if (_isDisposed) {
115+ return ;
85116 }
117+ _isDisposed = true ;
118+ super .dispose ();
86119 }
87120}
0 commit comments