Home > other >  What is wrong with my Shared preference? It wont remember my selected strokeWidth
What is wrong with my Shared preference? It wont remember my selected strokeWidth

Time:07-21

I am trying to make a doodle app and im trying to get my selections on strokeWidth carry over to a reboot of the app. I tried the code below, but it doesnt seem to be working. Can someone tell me what im doing wrong or point me in the right direction?

List<TouchPoints> pointsList = [];
  double opacity = 1.0;
  StrokeCap strokeType = StrokeCap.round;
  double strokeWidth = 3.0;
  Color selectedColor = Colors.black;

  Future<void> _pickStroke() async {
    //Shows AlertDialog
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      strokeWidth = (prefs.getDouble("strokeWidth") ?? 0);
      prefs.setDouble("strokeWidth", strokeWidth);
    });
    return showDialog<void>(
      context: context,

      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Choose stroke'),
          //Creates four buttons to pick stroke value.
          actions: <Widget>[
            //Resetting to default stroke value
            TextButton(
              child: Icon(
                Icons.brush,
                size: 12,
              ),
              onPressed: () {
                strokeWidth = 3.0;
                Navigator.of(context).pop();
              },
            ),

CodePudding user response:

you are not saving value when clicking.

just use it like :

 onPressed: () {
                strokeWidth = 3.0;
                prefs.setDouble("strokeWidth", strokeWidth);
                Navigator.of(context).pop();
              },

CodePudding user response:

  prefs.setDouble("strokeWidth", strokeWidth);

You should set value to shared prefrence on onPressed()

 List<TouchPoints> pointsList = [];
      double opacity = 1.0;
      StrokeCap strokeType = StrokeCap.round;
      double strokeWidth = 3.0;
      Color selectedColor = Colors.black;
    
      Future<void> _pickStroke() async {
        //Shows AlertDialog
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          strokeWidth = (prefs.getDouble("strokeWidth") ?? 0);
          prefs.setDouble("strokeWidth", strokeWidth);
        });
        return showDialog<void>(
          context: context,
    
          barrierDismissible: false, // user must tap button!
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text('Choose stroke'),
              //Creates four buttons to pick stroke value.
              actions: <Widget>[
                //Resetting to default stroke value
                TextButton(
                  child: Icon(
                    Icons.brush,
                    size: 12,
                  ),
                  onPressed: () {
                    strokeWidth = 3.0;
                    prefs.setDouble("strokeWidth", strokeWidth);
                    Navigator.of(context).pop();
                  },
                ),
  • Related