flutter - 消费者对配置文件/发布版本中 ChangeNotifier 的更改没有反应
问题描述
我有一个从 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)
解决方案
我已经发现发生了什么,一些子小部件正在构建方法中处理未来:
@override
Widget build(BuildContext context) {
return FutureProvider<FutureBundle>.value(
value: _bloc.getChannels(),
initialData: FutureBundle(state: BundleState.LOADING),
catchError: (context, error) {
return FutureBundle(state: BundleState.ERROR, data: error);
},
child: Consumer<FutureBundle>(builder: (context, bundle, view) {
我在此响应参考之后更改了此实现,并且一切都再次正常工作:
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ChangeNotifierProvider<WaitingBloc>.value(
value: _bloc,
child: Consumer<WaitingBloc>(builder: (context, bloc, child) {
推荐阅读
- r - 使用 SLURM bash 脚本进行并行化和资源分配
- flutter - 使用 Positioned 小部件在 ListView.builder 项目小部件上添加小部件
- laravel - 获取徽标以显示在电子邮件标题中
- javascript - 如何在 IE 11 中实现扩展运算符?
- sql - Oracle 中的 IF ELSE 条件需要帮助
- neo4j - Neo4j 通过原生 api 创建索引的问题
- vb.net - 如何进行循环以查找匹配的字符串
- azure - 有没有办法在任务运行时(在完成之前)从 Azue Batch 任务中动态流式传输数据
- c# - 使用模型等待多个调用
- javascript - 使用 Javascript 和 Acrobat DC 在 PDF 中提取超过 49 页时出错