Flutter -如何在Flutter项目的共享首选项中保存布尔值?

oxiaedzo  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(103)

我已经处理这个问题一个星期了。
我不知道如何在共享首选项中保存一个布尔值。我真的需要它用于多个项目。
有人能告诉我,我是如何在代码中保存Checkboxlisttile的bool值的吗?
请修改我的代码:)

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/src/material/checkbox_list_tile.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Gym',
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({super.key});

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

//
//
//
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  @override
  void initState() {
    _loadData();
    super.initState();
  }

  _loadData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool boolValue = prefs.getBool('option');
  }

  @override
  _saveBool() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
  }

  var _test = false as bool;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CheckboxListTile(
          value: _test,
          onChanged: (val) {
            setState(() {
              _test = !_test;
              _saveBool();
            });
          },
        ),
      ),
    );
  }
}

我看了yt的视频和stackoverflow的帖子,没有任何效果。

s8vozzvw

s8vozzvw1#

以下是您答案示例:

class SharedPreference {
  static SharedPreferences? sharedPreferences;

  static Future<void> init() async {
    sharedPreferences = await SharedPreferences.getInstance();
  }

  
  static void setOptionState(bool optionState) async {
    await sharedPreferences!.setBool('option', optionState);
  }

  static bool getOption() {
    return sharedPreferences!.getBool('option') ?? false;
  }
}

在需要保存bool的地方使用setter和getter:

SharedPreferences.setOptionState(true);

var yourBoolean = SharedPreferences.getOption();

编辑:根据你在评论中说的你想要的确切代码,下面是你应该做的。注意,在这些代码之前,像我一开始说的那样创建SharedPreference类:

import 'package:flutter/material.dart';
import 'package:you_project/shared_preference.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async{
  await SharedPreference.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Gym',
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({super.key});

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

//
//
//
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  var _test = false;
  @override
  void initState() {
    _loadData();
    super.initState();
  }

  _loadData() async {
    _test = SharedPreferences.getOption();
  }

  @override
  _saveBool(bool testBool) async {
    SharedPreferences.setOptionState(testBool);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CheckboxListTile(
          value: _test,
          onChanged: (val) {
            setState(() {
              _test = !_test;
              _saveBool(_test);
            });
          },
        ),
      ),
    );
  }
}

快乐编码...

相关问题