how to change the state inside FutureBuilder

Viewed 404

i just need when the user open the screen the notification icon button change when he click, it's value is coming from shared preferences. the problem is the icon is never changed! the initState code:

  @override
  void initState() { 
    super.initState();
    _isActiveNotification = _notificationGetState();
   
  }

_notificationGetState function is:

  //getting notification on/off
  Future<bool> _notificationGetState() async {
    final SharedPreferences _prefs = await SharedPreferences.getInstance();

    return _prefs.getBool('notification') ?? true;
  }

_isActiveNotification variable is:

late Future<bool> _isActiveNotification;

the class of the notification icon button is:

class _NoificationActivationButton extends StatefulWidget {
  _NoificationActivationButton();
  @override
  _NoificationActivationButtonState createState() =>
      _NoificationActivationButtonState();
}
    
    
   class _NoificationActivationButtonState
            extends State<_NoificationActivationButton> {
          @override
          Widget build(BuildContext context) {
            return FutureBuilder<bool>(
                //function haveing the return value
                future: _isActiveNotification,
                builder: (context, snapshot) {
                  bool data = snapshot.data!;
                  return IconButton(
                    icon: Icon(
                      data
                          ? Icons.notifications_active_outlined
                          : Icons.notifications_off_outlined,
                      color: Colors.white,
                      size: 40,
                    ),
                    onPressed: () {
                      setState(() {
                         data = !data;
                       });
                    },
                  );
                });
          }
2 Answers

just call setstate

onPressed: () {
 data = !data;

// just call setstate((){});
},

Make data a global state.

NOTE: I'm only assuming that you will only call _notificationGetState once (in initState).

Sample...

class _NoificationActivationButtonState
    extends State<_NoificationActivationButton> {
  final bool _isOtherVersion = true;

  late Future<bool> _isActiveNotification;

  bool? _data;

  @override
  void initState() {
    super.initState();
    _isActiveNotification = _notificationGetState();
  }

  //getting notification on/off
  Future<bool> _notificationGetState() async {
    final SharedPreferences _prefs = await SharedPreferences.getInstance();
    return _isOtherVersion
        ? _prefs.getBool('notification') ?? true
        : _data = _prefs.getBool('notification') ?? true;
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<bool>(
      //function haveing the return value
      future: _isActiveNotification,
      builder: (context, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return const CircularProgressIndicator();
        }

        if (_isOtherVersion && _data == null) {
          _data = snapshot.data;
        }

        return IconButton(
          icon: Icon(
            _data!
                ? Icons.notifications_active_outlined
                : Icons.notifications_off_outlined,
            color: Colors.white,
            size: 40,
          ),
          onPressed: () => setState(() => _data = !_data!),
        );
      },
    );
  }
}
Related