尝试使用 bool 共享首选项

trying Shared-preferences with bool

我是第一次使用Shared-preferences,但是当我点击图标并在离开程序时保存值时无法在true和false之间切换,有谁知道出了什么问题吗?

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Shared preferences demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Shared preferences demo'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  bool select = true;
  @override
  void initState() {
    super.initState();
    _loadCounter();
  }
  _loadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('select', true);
  }
  _incrementCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    //Return bool
    bool boolValue = prefs.getBool('select');
    return boolValue;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title),),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '$select',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          child: Icon(Icons.add),
      ),);}}

修改_incrementCounter方法以获取bool值,即从共享首选项中获取的未来值

class _MyHomePageState extends State<MyHomePage> {
  bool select;

  @override
  void initState() {
    super.initState();
    setInitialValue();
  }

  setInitialValue() async {
    final value = await _incrementCounter();
    setState(() {
      select = value?? true;
    });
  }

  _loadCounter(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('select', value);
  }

  Future<bool> _incrementCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    //Return bool
    bool boolValue = prefs.getBool('select');
    return boolValue;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '$select',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          _loadCounter(!select);
          final value = await _incrementCounter();
          setState(() {
            select = value;
          });
        },
        child: Icon(Icons.add),
      ),
    );
  }
}