gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:59:53 24 4
gpt4 key购买 nike

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

我已经尝试了一些针对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

最佳答案

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

关于flutter - 找不到消费者提供者的祖先或找不到提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58082966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com