我有一个从 ChangeNotifier 扩展的类,它管理一个小部件的状态:
MainSection _section = MainSection.SETUP;
MainSection get section => _section;
set section(MainSection value) {
_section = value;
// some code
notifyListeners();
}
正如我所说,我使用它来更改小部件的状态:
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MainBloc>.value(
value: _bloc,
child: Consumer<MainBloc>(builder: (context, bloc, child) {
_bloc = bloc;
var body;
switch (_bloc.section) {
case MainSection.SETUP:
body = _widgetFactory.createSetupWidget();
break;
case MainSection.WAITING:
body = Column(
children: <Widget>[
Expanded(
child: _widgetFactory.createWaitingWidget(),
),
_getBottomBar()
],
);
break;
这种机制运行良好,因为我更新了应用程序以使用最新的 Flutter 版本。现在在调试模式下,它在所有情况下都可以正常工作,但在配置文件或发布模式下,它在应用程序中的特定点不起作用,这意味着它适用于某些状态更改,但对于特定更改不起作用。我不知道有什么影响。
为什么我说它不起作用:我更改了变量,调用 notifyListeners() 但消费者没有得到通知。
我正在使用提供程序依赖版本 4.3.1
颤振医生:
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.5, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2019.2)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
[!] VS Code (version 1.47.3)
✗ Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)