flutter 状态管理
For help getting started with Flutter, view our online documentation.
flutter中用rxdart实现状态管理,主题切换
flutter 状态管理
For help getting started with Flutter, view our online documentation.
退出app后再打开会恢复到日间主题,我尝试使用SharedPreferences来储存一个bool值来记录主题,想要在app初始化的main方法里进行如下处理:
var themeBloc;
void main() async {
getLoginInfo();
// await checkChina();
runApp(BlocProvider(child: App()));
initTheme();
SystemUiOverlayStyle systemUiOverlayStyle =
SystemUiOverlayStyle(statusBarColor: Colors.transparent);
SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}
void initTheme() async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.getBool("isNight");
print(sp.getBool("isNight"));
themeBloc.changeTheme(sp.getBool("isNight"));
}
出现如下报错:
出现异常。
NoSuchMethodError (NoSuchMethodError: The method 'changeTheme' was called on null.
Receiver: null
Tried calling: changeTheme(true))
sp.getBool("isNight")是有值的, themeBloc.changeTheme方法来自于源码如下:
import 'package:rxdart/rxdart.dart';
class ThemeChangeBloc {
bool _isNight = true;
var _subject = BehaviorSubject<bool>();
Stream<bool> get isNight => _subject.stream;
void changeTheme(bool value) => _subject.add(_isNight = value);
void disposed() {
_subject.close();
}
}
我技术比较垃圾,希望您在工作之余闲暇之时为我解惑,不胜感激。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.