找不到消费者提供者的祖先或找不到提供者

时间:2019-09-24 14:48:11

标签: flutter dart

我有一个多提供程序配置,我在mainApp中传递了提供程序,并在以后使用了ConsumerProvider。但我得到祖先未找到错误。相同的设置适用于其他视图,但可能由于导航而造成问题

我已经尝试了一些针对stackoverflow中类似问题发现的选项,这些选项表明将提供者转移到其他位置,并查看提供的上下文,但是找不到任何解决方案

首先是我的Provider.dart文件

List<SingleChildCloneableWidget> providers = [
  ...independentServices,
  ...dependentServices,
];

List<SingleChildCloneableWidget> independentServices = [
  Provider.value(value: FirebaseNewsService()),
  Provider.value(value: FirebaseEventsService())
];

List<SingleChildCloneableWidget> dependentServices = [
  ProxyProvider<FirebaseNewsService, NewsListModel>(
      builder: (context, newsService, _) {
    return NewsListModel(newsService: newsService);
  }),
  ProxyProvider<FirebaseNewsService, NewsCreateModel>(
    builder: (context, newsService, _) {
      return NewsCreateModel(newsService: newsService);
    },
  ),
  ProxyProvider<FirebaseEventsService, EventsListModel>(
      builder: (context, eventsService, _) {
    return EventsListModel(eventsService: eventsService);
  }),
];

接下来是main.dart文件

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: providers,
          child:MaterialApp(
        title: 'MyApp',
        initialRoute: RoutePaths.Home,
        onGenerateRoute: Router.generateRoute, )
    );
  }
}

接下来是进行路由的router.dart文件

class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {

// this is working
      case RoutePaths.Home:
        return MaterialPageRoute(builder: (context) {
          NewsListModel model = Provider.of(context);
          return ChangeNotifierProvider<NewsListModel>.value(
              value: model, child: NewsPage());
        });
        break;

      case RoutePaths.Events:
        return MaterialPageRoute(builder: (_) {
EventListModel model = Provider.of(context);
          return ChangeNotifierProvider<EventListModel>.value(
              value: model, child: EventsListPage());
        });
        break;

我的主页文件

class NewsPage extends StatelessWidget {
  final String _tab1title = allTranslations.text('newsPage.tabtitleone');
  final String _tab2title = allTranslations.text('newsPage.tabtitletwo');
  static const _tablength = 2;
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _tablength,
      child: Scaffold(
        drawer: Menu(),   //Maybe Menu is having a different context
        body: NestedScrollView(
        ...
        body: Tabbarview(children: [] . // this works fine 

在问题小部件中的事件列表.dart文件

class EventListPage extends statelessWidget {
Widget build(BuildContext context) {
    EventListModel model = Provider.of(context);

    return  Scaffold(drawer: Menu(), appBar: AppBar(), body: ChangeNotifierProvider<EventsListModel>.value(
      value: model,
      child: Consumer<EventsListModel>(
        builder: (context, model, child) => model.busy
            ? Center(
                child: CircularProgressIndicator(),
              )
            : Column(mainAxisSize: MainAxisSize.max, children: <Widget>[
                SmartRefresher(
                    //key: EventsPageModel.eventsFollowKey,
                    controller: model.refreshController,
                    enablePullDown: true,
                    header: WaterDropMaterialHeader(
                      backgroundColor: Theme.of(context).primaryColor,
                    ),
                    enablePullUp: true,
                    onRefresh: model.onRefresh,
                    onLoading: model.onLoading,
                    child: buildchild(model, context)),
              ]),
      ),
    );
}

我总是得到找不到消费者的祖先或 找不到正确的提供者。我在哪里做错了。

NewsListModel同样适用

1 个答案:

答案 0 :(得分:0)

是区分大小写的导入错误https://github.com/microsoft/TypeScript/issues/21736。结束此问题