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;
                       });
                    },
                  );
                });
          }
5
  • 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
    Commented Apr 22, 2021 at 8:35
  • the same issue!
    – user15543853
    Commented Apr 22, 2021 at 8:51
  • You sure? Can you update the code?
    – rickimaru
    Commented Apr 22, 2021 at 8: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
    – user15543853
    Commented Apr 22, 2021 at 9:10
  • Actually, it's just the same. If you want to set the data in FutureBuilder, see my answer (_isOtherVersion = true).
    – rickimaru
    Commented Apr 22, 2021 at 9:21

2 Answers 2

0

just call setstate

onPressed: () {
 data = !data;

// just call setstate((){});
},
0
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!),
        );
      },
    );
  }
}
2
  • thanks a lot!, i have created another solution down below. i hope you see it, hearing your opinion :) @rickimaru
    – user15543853
    Commented Apr 23, 2021 at 7:29
  • 1
    @someone Nice. I think you can remove snapshot.data != snapshot.data; inside the setState.
    – rickimaru
    Commented Apr 23, 2021 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.