颤音:可以使用溪流构造器检测变化发生在bool变量中吗?如果是,怎么样?如果没有,我可以使用什么方法?

发布于 2025-02-08 16:49:32 字数 1671 浏览 3 评论 0原文

我想管理应用程序的主题。我正在使用共享流程来存储指示darkmode/lightmode的布尔值。每当用户更改模式时,Bool值都会更改。每当BOOL值更改时,我想更改应用程序的主题。我正在尝试使用StreamBuilder,但它给了我错误。我是Flutter的新手,我不知道它是否可以使用。如果是,如何使用它?如果否,那么在布尔值更改时,我该如何更改主题?有人可以帮忙吗? 这是我的代码,

class SharedPreference {
  bool _darkmode  = false;

  get darkMode {
    return _darkmode;
  }

  setTheme(bool value) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setBool('isdark', value);
    _darkmode = value;
  }

  Future<bool> getTheme() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getBool('isdark') ?? false;
  }

}

// button to change mode

Switch.adaptive(
  value: off_on,
  activeColor: Colors.deepOrange,
  onChanged: (value) {
    setState(() => this.off_on = value);
    SharedPreference().setTheme(value);
  } 
),


// main

class _MyappState extends State<Myapp> {
  
  @override
  Widget build(BuildContext context) {
    return  StreamBuilder(
      stream: SharedPreference().darkMode,
      builder: (context, snapshot) {
        return MaterialApp(
          // theme: ThemeData(
          //   fontFamily: 'OpenSans',
          //   primarySwatch: Colors.deepOrange,
          // ),
          theme: ModeSpecifications.themeData(SharedPreference().darkMode, context),
          navigatorKey: navigatorKey,
          initialRoute: '/mainpage',
          routes: {
            '/mainpage': (context)=> MainPage(),
          },
        );
      }
    );
  }
}

我收到以下错误: 类型的“ bool”不是类型的子类型,“流”&lt; object?&gt ;?'

和 在Channel插件上找不到用于GetAll的实现。

未经手的例外:缺少Pluginexception (

I want to manage the theme of my app. I am using SharedPreferences to store a bool value which indicate darkmode/lightmode. Whenever the user changes the mode the bool value will change. I want to change the theme of my app whenever the bool value changes. I am trying to use a streambuilder but it gives me error. I am new to flutter and I don't know whether it can be used or not. If yes, how to use it? If no, then how can I change theme while the bool value changes ? Can someone help?
Here is my code

class SharedPreference {
  bool _darkmode  = false;

  get darkMode {
    return _darkmode;
  }

  setTheme(bool value) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setBool('isdark', value);
    _darkmode = value;
  }

  Future<bool> getTheme() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getBool('isdark') ?? false;
  }

}

// button to change mode

Switch.adaptive(
  value: off_on,
  activeColor: Colors.deepOrange,
  onChanged: (value) {
    setState(() => this.off_on = value);
    SharedPreference().setTheme(value);
  } 
),


// main

class _MyappState extends State<Myapp> {
  
  @override
  Widget build(BuildContext context) {
    return  StreamBuilder(
      stream: SharedPreference().darkMode,
      builder: (context, snapshot) {
        return MaterialApp(
          // theme: ThemeData(
          //   fontFamily: 'OpenSans',
          //   primarySwatch: Colors.deepOrange,
          // ),
          theme: ModeSpecifications.themeData(SharedPreference().darkMode, context),
          navigatorKey: navigatorKey,
          initialRoute: '/mainpage',
          routes: {
            '/mainpage': (context)=> MainPage(),
          },
        );
      }
    );
  }
}

I got the following errors:
type 'bool' is not a subtype of type 'Stream<Object?>?'

and
Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences_android)

Can someone help to resolve the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2025-02-15 16:49:32

您可以使用streamController来创建streamBuilder可以列出的流。这应该起作用。

class SharedPreference {
  final _controller = StreamController<bool>();

  Stream<bool> get darkMode async* {
    yield* _controller.stream;
  }

  void setTheme(bool value) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setBool('isdark', value);
    _controller.add(value); // update value here.
  }

  Future<bool> getTheme() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getBool('isdark') ?? false;
  }

  // call when user closes the app or something.
  void dispose() => _controller.close();
}

You can use a StreamController to create a stream that streamBuilder could list to. This should work.

class SharedPreference {
  final _controller = StreamController<bool>();

  Stream<bool> get darkMode async* {
    yield* _controller.stream;
  }

  void setTheme(bool value) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setBool('isdark', value);
    _controller.add(value); // update value here.
  }

  Future<bool> getTheme() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getBool('isdark') ?? false;
  }

  // call when user closes the app or something.
  void dispose() => _controller.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文