在小部件init中设置提供程序不起作用

时间:2019-09-19 04:32:28

标签: flutter dart state-management

我试图在初始化小部件时设置提供程序变量,以使小部件具有适当的数据。这样做时出现错误。这是代码:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      builder: (context) => ListManager(),
      child: MaterialApp(
        title: 'crud-to-do',
        home: Home(), // Home calls ListWidget()
      ),
    );
  }
}

class ListWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final listManager = Provider.of<ListManager>(context);
      listManager.list = [
        ListItem(title: 'myTtitle', description: 'My desc', id: '1324j323e')
      ];

    if (listManager.list.isEmpty) return Text("hey");
    return Text(listManager.list[0].title);
  }
}

这可行,但出现错误:

flutter: ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
flutter: The following assertion was thrown while dispatching notifications for ListManager:
flutter: setState() or markNeedsBuild() called during build.
flutter: This ChangeNotifierProvider<ListManager> widget cannot be marked as needing to build because the
flutter: framework is already in the process of building widgets.  A widget can be marked as needing to be
flutter: built during the build phase only if one of its ancestors is currently building. This exception is
flutter: allowed because the framework builds parent widgets before children, which means a dirty descendant
flutter: will always be built. Otherwise, the framework might not visit this widget during this build phase.
flutter: The widget on which setState() or markNeedsBuild() was called was:
flutter:   ChangeNotifierProvider<ListManager>
flutter: The widget which was currently being built when the offending call was made was:
flutter:   ListWidget
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Element.markNeedsBuild.<anonymous closure> 
package:flutter/…/widgets/framework.dart:3680
flutter: #1      Element.markNeedsBuild 
package:flutter/…/widgets/framework.dart:3695
flutter: #2      State.setState

因此,我因此尝试将initState包裹在listManager.list的周围,像这样:

@override
void initState() {
  listManager.list = [
        ListItem(title: 'myTtitle', description: 'My desc', id: '1324j323e')
  ];
}

但是我收到一条错误消息:

  

未引用声明'initState'。

在两种情况下我都做错了什么?设置provider的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

在您的构建方法中,使用Provider.of<ListManager>(context, listen: false)阻止重建。

默认情况下,listen设置为true,并且在模型更改时将触发重建,这将在build()方法或initState()中引发异常。