颤音:可以使用溪流构造器检测变化发生在bool变量中吗?如果是,怎么样?如果没有,我可以使用什么方法?
我想管理应用程序的主题。我正在使用共享流程来存储指示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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
streamController
来创建streamBuilder
可以列出的流。这应该起作用。You can use a
StreamController
to create a stream thatstreamBuilder
could list to. This should work.