Flutter Mobx颤振观测值未更新该值。每次我得到默认值时
您好,我是新来的颤栗,我正在尝试使用mobx状态管理Flutter Mobx颤振观测值未更新该值。每次我得到默认值时,flutter,mobx,state-management,Flutter,Mobx,State Management,您好,我是新来的颤栗,我正在尝试使用mobx状态管理 import 'package:mobx/mobx.dart'; part 'counter.g.dart'; class Counter = _Counter with _$Counter; abstract class _Counter with Store { @observable String hello = 'Hello'; @action void changeName(_name){ hello
import 'package:mobx/mobx.dart';
part 'counter.g.dart';
class Counter = _Counter with _$Counter;
abstract class _Counter with Store {
@observable
String hello = 'Hello';
@action
void changeName(_name){
hello = _name;
}
}
和.g.dart也使用更新的值生成。
我正在启动动作,就像(动作调用部分)
渲染部分:
final Counter counter = Counter();
return Scaffold(
body: Observer(builder: (_) {
return Container(
height: 100.0,
width: 100.0,
decoration: BoxDecoration(),
child: Text(counter.hello),
);
}),
每次我得到counter.hello作为“hello”(默认值)。
我没有得到“更新值”
->Action changeName正在触发(我已对其进行调试)
请帮帮我。这部分
final Counter counter = Counter();
return Observer(builder: (_)=> InkWell(
onTap: () {
counter.changeName("updated value");
}
这部分呢
final Counter counter = Counter();
return Scaffold(
body: Observer(builder: (_) {
return Container(
height: 100.0,
width: 100.0,
decoration: BoxDecoration(),
child: Text(counter.hello),
);
}),
是否在同一个小部件树中?我注意到,在这两个部分中,您都实例化了计数器(),
要更新文本,您需要使用该类的同一实例。太好了!!。。这很好,谢谢你让我知道我的错误。
final Counter counter = Counter();
return Scaffold(
body: Observer(builder: (_) {
return Container(
height: 100.0,
width: 100.0,
decoration: BoxDecoration(),
child: Text(counter.hello),
);
}),