如何在屏幕上访问MobX存储,就像在React中注入存储一样?
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _store = exStore();
void init() {
_store.setTitle('foo');
}
}
导航到主屏幕时,我需要获取数据标题
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final _store = exStore();
void init() {
print(_store.title); // return null
}
}
我需要_store.title返回'foo'。
答案 0 :(得分:0)
很简单,您只需要在任何小部件外部创建实例即可,使其具有全局性,如下所示。
store = exStore();
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
void init() {
store.setTitle('foo');
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
void init() {
print(store.title); // return null
}
}