为什么在将 flutter 应用程序存储库导入为依赖项时会出现此错误?

问题描述 投票:0回答:1

我正在将 flutter 与 nfcmanager 结合使用作为项目的一部分。我想使用 nfcmanager 应用程序的“现实世界示例”中的类,该应用程序已在我的机器上进行了测试,并且在我的代码中运行得很好,因此我添加了存储库的一个分支作为依赖项,并已成功将其导入. 但是,当我尝试在导入的代码(TagReadPage)中运行主类之一时出现问题:

class TagReadPage extends StatelessWidget { static Widget withDependency() => ChangeNotifierProvider<TagReadModel>( create: (context) => TagReadModel(), child: TagReadPage(), ); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Tag - Read'), ), body: ListView( padding: EdgeInsets.all(2), children: [ FormSection( children: [ FormRow( title: Text('Start Session', style: TextStyle( color: Theme.of(context).colorScheme.primary)), onTap: () => startSession( context: context, handleTag: Provider.of<TagReadModel>(context, listen: false) .handleTag, ), ), ], ), // consider: Selector<Tuple<{TAG}, {ADDITIONAL_DATA}>> Consumer<TagReadModel>(builder: (context, model, _) { final tag = model.tag; final additionalData = model.additionalData; if (tag != null && additionalData != null) return _TagInfo(tag, additionalData); return SizedBox.shrink(); }), ], ), ); } }

如果您需要,我可以包含引用的其他类。

当我尝试使用导航器运行此类时:

Navigator.push(context, MaterialPageRoute(builder: (context) => TagReadPage()));

我在终端上收到无用的错误消息,但在我的应用程序内出现此错误:

Error: Could not find the correct Provider<TagReadModel> above this Consumer <TagReadModel> Widget This happens because you used a 'BuildContext' that does not include the provider of your choice. There are a few common scenarios: You added a new provider in you main.dart and performed a hot reload, to fix perform a hot restart The provider you are trying to read is in a different route Providers are scoped so if you insert of provider inside a route, then other routes will not be able to access that provider You used a BuildContext that is an ancestor of the provider you are trying to read Make sure than Consumer<TagReadModel> us under your MultiProvider/Provider<TagReadModel>, this usually happens when you are creating a provider and trying to read it immediately For example, instead of: Widget build(BuildContext context){ return Provider<Example>( create:(_)=>Example(), child: Text(context.watch<Example>().toString()),)} Consider using builder like so: Widget build(BuildContext context){ return Provider<Example>( create:(_)=>Example(), return Text(context.watch<Example>().toString()

我运行了热重启并遇到了同样的问题。我知道这可能是一个简单的修复,但我对 flutter 还很陌生,所以一些关于如何修复这个问题的建议以便我可以运行该类会很棒。

flutter dependencies repo pubspec.yaml
1个回答
0
投票

根据经验,每当您使用

Consumer

Selector
Provider.of(context)
小部件时,它都会尝试通过遍历小部件树直至根来找到相应的
Provider
。在您的情况下,
Consumer
小部件将无法在其上方的小部件树中找到相应的
Provider
小部件,直到它到达顶部。
为了在小部件树中找到它们的位置,小部件使用 

BuildContext

。重要的是,

Provider
小部件必须在包含对
Provider
的依赖项并使用不同的
BuildContext
的小部件之上声明。
示例代码的作者针对这个问题有一个解决方案:

static Widget withDependency() => ChangeNotifierProvider<TagReadModel>( create: (context) => TagReadModel(), child: TagReadPage(), );

此方法将 
TagReadPage

实例包装在

ChangeNotifierProvider
中。因此,如果您使用此静态方法创建它,则提供程序应该出现在小部件树中。但是,您已经构建了到此页面的路由,而没有使用此方法。你应该改变它:
Navigator.push(context, MaterialPageRoute(builder: (context) => TagReadPage()));

类似:

Navigator.push(context, MaterialPageRoute(builder: (context) => TagReadPage.withDependency()));

希望这有效并有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.