2

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;
                       });
                    },
                  );
                });
          }
  • Make `data` a global variable of class `_NoificationActivationButtonState` and assign it in `return data = _prefs.getBool('notification') ?? true;`. Don't forget to remove `bool data = snapshot.data!;`. – rickimaru Apr 22 '21 at 08:35
  • the same issue! –  Apr 22 '21 at 08:51
  • You sure? Can you update the code? – rickimaru Apr 22 '21 at 08:55
  • i am using FutureBuilder for avoid your solution, as FutureBuilder take the future variable building the widget according the Future Value. if you tried the code you will understand what i am saying. @rickimaru –  Apr 22 '21 at 09:10
  • Actually, it's just the same. If you want to set the `data` in `FutureBuilder`, see my answer (`_isOtherVersion = true`). – rickimaru Apr 22 '21 at 09:21

2 Answers2

0

just call setstate

onPressed: () {
 data = !data;

// just call setstate((){});
},
Gbenga B Ayannuga
  • 2,407
  • 3
  • 17
  • 38
0

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!),
        );
      },
    );
  }
}
rickimaru
  • 2,275
  • 9
  • 13
  • thanks a lot!, i have created another solution down below. i hope you see it, hearing your opinion :) @rickimaru –  Apr 23 '21 at 07:29
  • 1
    @someone Nice. I think you can remove `snapshot.data != snapshot.data;` inside the `setState`. – rickimaru Apr 23 '21 at 08:45