0

This is my code.

I want to use shared_preferences in this code so that the user can choose their own font size and that font size is applied throughout the app.

However, I'm stuck at the part where I use shared_preferences to save it. I tried referring to the article in the link below, but it is not saved. A null value is still returned.

Flutter, How to save font size options into sharedpreference?

How do I use shared_preferences to save the font size?

class MyApp2 extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp2> {
  int? _changeFontSize;
  final List<int> _fontSizeList = [10, 11,12,13,14,15];

  @override
  void initState() {
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      //Retrieving font size
      getFontSize().then((value) => setState(() {
        _changeFontSize = value as int?;
      }));
    });
    super.initState();
  }

  void addDefaultValueToSharedPreferences() async {
    final sharedPreferences = await SharedPreferences.getInstance();
    await sharedPreferences.setInt('fontsize', 1);
  }
  Future<int?> getFontSize() async {
    final sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.getInt('fontsize');
  }

  Future<void> updateFontSize(int updatedSize) async {
    final sharedPreferences = await SharedPreferences.getInstance();
    await sharedPreferences.setInt('fontsize', updatedSize);
  }



  @override
  Widget build(BuildContext context) {
    print(_changeFontSize);
    print(SharedPreferences.getInstance());
    return Center(
          child: Column(
            children: [
              Card(
                margin: EdgeInsets.only(bottom: 3),
                child: ListTile(
                  title: Text("Font Size"),
                  trailing: DropdownButtonHideUnderline(
                    child: DropdownButton(
                      isExpanded: false,
                      value: _changeFontSize,
                      items: _fontSizeList.map((myFontSize) {
                        return DropdownMenuItem(
                          child: Text(myFontSize.toString()),
                          value: myFontSize,
                        );
                      }).toList(),
                      onChanged: (value) async {
                        setState(() {
                          _changeFontSize = value as int?;
                        });
                        //Updating font size
                        await updateFontSize(value as int);
                      },
                      hint: Text("Select FontSize"),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
  }
}
eno2
  • 73
  • 11

1 Answers1

0

You have to add return to getFontSize method

Future<int?> getFontSize() async {
    final sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getInt('fontsize');
  }
Eugene Kuzmenko
  • 1,216
  • 10
  • 12