Search code examples
fluttersparklines

How to use sharedpreference with sparkline, in flutter?


Hello I tried to make a dynamic chart with sparkline, but I can't use my double variable from sharedpreference... when I use double variable I have an error : only static membre can be accessed in initializer. But I search to draw a dynamic chart not a static.

thank you

example :

 load_my_variable_double() async {


    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      my_variable_double= (prefs.getDouble('my_variable_double'))??0;

    });
  }

     var data = [my_variable_double,];  //only static membre can be accessed in initializer.
...    

       Sparkline
                            (
                            data: data,
                            lineWidth: 5.0,
                            lineColor: Colors.greenAccent,
                          )

Solution

  • try the following, it should work

     var data;
     load_my_variable_double() async {
    
    
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          my_variable_double= (prefs.getDouble('my_variable_double'))??0;
          data = [my_variable_double,];
    
        });
      }
    
    
    ...    
    
           Sparkline
                                (
                                data: data,
                                lineWidth: 5.0,
                                lineColor: Colors.greenAccent,
                              )