- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Flutter 中开发一个应用程序(我还是个新手),但遇到以下错误:
Error: Could not find the correct Provider<List<Category>> above this Exercises Widget
This likely happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- 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 that Exercises is under your MultiProvider/Provider<List<Category>>.
This usually happen 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(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>()),
),
}
```
consider using `builder` like so:
```
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context) {
// No longer throws
return Text(context.watch<Example>()),
}
),
我一直在网上查看,这显然与我在 execution_add.dart 中调用 Provider.of
Widget build(BuildContext context) {
return _isLoading
? Loading()
: MultiProvider(
providers: [
StreamProvider<List<Exercise>>(
create: (context) => DatabaseService().exercises,
),
StreamProvider<List<Category>>(
create: (context) => DatabaseService().categories,
),
ChangeNotifierProvider<ExerciseFilter>(
create: (context) => ExerciseFilter(isActive: true),
)
],
child: Scaffold(
appBar: AppBar(
title: Text('Exercises'),
elevation: 0.0,
actions: _buildActions(),
),
body: ExerciseList(),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.black,
child: Icon(Icons.add, color: Colors.white),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ExercisesAdd(),
),
);
},
),
),
);
}
}
练习_add.dart
@override
Widget build(BuildContext context) {
final cats = Provider.of<List<Category>>(context);
print(cats.length);
return Scaffold(
appBar: AppBar(
title: Text('Add Exercise'),
elevation: 0.0,
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
SizedBox(height: 20.0),
TextFormField(
decoration:
textInputDecoration.copyWith(hintText: 'Exercise Name'),
validator: (value) {
if (value.isEmpty) {
return 'Exercise name is required';
}
return null;
},
onChanged: (value) {
setState(() {
exerciseName = value;
});
},
),
SizedBox(height: 20.0),
Theme(
data: Theme.of(context).copyWith(canvasColor: Colors.white),
child: DropdownButtonFormField<String>(
decoration: dropdownDecoration,
value: exerciseCategory,
onChanged: (value) {
setState(() {
exerciseCategory = value;
});
},
items: categories.map<DropdownMenuItem<String>>((value) {
return DropdownMenuItem<String>(
value: value.name,
child: Text(value.name),
);
}).toList(),
),
),
SizedBox(height: 20.0),
RaisedButton(
elevation: 0,
color: Colors.black,
child: Text(
'Add Exercise',
style: TextStyle(color: Colors.white),
),
onPressed: () async {
if (_formKey.currentState.validate()) {
bool failed = false;
String uid = await _auth.getCurrentUser();
if (uid != null) {
dynamic result = DatabaseService(uid: uid)
.addExercise(exerciseName, exerciseCategory);
if (result != null) {
Navigator.pop(context);
} else {
failed = true;
}
} else {
failed = true;
}
if (failed) {
setState(() {
error = 'Failed to add exercise. Please try again';
});
}
}
},
),
SizedBox(height: 12.0),
Text(
error,
style: TextStyle(color: Colors.red, fontSize: 14.0),
),
],
),
),
),
),
);
数据库服务().练习
List<Category> _categoryListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
return Category(name: doc.data['category'] ?? '');
}).toList();
}
Stream<List<Category>> get categories {
return categoryCollection
.orderBy('category')
.snapshots()
.map(_categoryListFromSnapshot);
}
注意:StreamProvider 和 MultiProvider 等都是 'provider' 包的一部分(我使用最新版本)
最佳答案
您收到的错误描述了您当前所处的场景。
- 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.
MultiProvider
在您在小部件树中使用的任何导航器上方。您可能正在使用
MaterialApp
为此,请移步
MultiProvider
并制作
MaterialApp
它是
child
.
关于Flutter StreamProvider 使用了一个 `BuildContext`,它是提供者的祖先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63315716/
我正在尝试构建一个页面大小三分之一的容器,但我收到错误 No MediaQuery ancestor could be found starting from the context that was
给定包含数据的表T_Person(姓名,父级) +--------+--------+| name | parent |+--------+--------+| john | peter |
假设输入 XML 为 Test Me 我想找到 title 和 author 的最低共同祖先。我在 BaseX 中尝试了以下代码: let $p := doc('t.xq
给定表 T_Person(姓名, parent ) 包含数据 +--------+--------+| name | parent |+--------+--------+| john | p
我在leetcode中找到了java中最低公共(public)祖先问题的解决方案。换句话说,问题是找到 p 和 q 的最低共同祖先,并且 BST 的根为根。这是我的代码。 public TreeNod
我知道如果这两个节点必须在二叉树中如何解决问题,但是如果它们不必在树中怎么办?如果树中只有一个或没有这些节点,则返回 None。 这是我的代码: # Definition for a binary t
我在 stackoverflow 上查看了很多其他答案,但找不到任何有效的东西,我要么得到根,要么返回 node1 本身,我不知道如何递归地执行此操作,并且已经尝试了很多次一切都以同样的方式结束。任何
如果所有元素都不同,则很容易在 BST 中找到最近的共同祖先。但是,如果某些值相同怎么办。到目前为止,我们只是比较节点的数据,仅此而已,但现在我们是否需要检查节点的地址而不仅仅是值? 最佳答案 是的,
问题:给定一个二叉搜索树 (BST),找到 BST 中两个给定节点的最低公共(public)祖先 (LCA)。 根据维基百科上 LCA 的定义:“最低公共(public)祖先定义在两个节点 v 和 w
我正在尝试通过自上而下的递归来解决二叉树的最低公共(public)祖先(LCA)问题。 我使用的方法是: IDEA: Find the node which has one of the desire
我被这个问题绊倒了。以下是我的做法: Lets say the two nodes are node1 and node2 For any node (lets say node1), find th
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
只是想知道以下算法在二叉搜索树中查找两个节点的最低公共(public)祖先的效率如何。 Node getLowestCommonAncestor (Node root, Node a, Node b)
如果我知道树中每个节点的邻接列表,那么如何找出该树中存在的任意两个节点的最低公共(public)祖先? 其实我想找出任意两个节点之间的距离,所以我想计算 LCA。有没有办法从邻接表中计算出来? T 中
这里的二叉树不一定是二叉搜索树。 该结构可以视为 - struct node { int data; struct node *left; struct node *right
以下是我对二叉搜索树的最低公共(public)祖先的实现。我有两个问题: 1) 时间复杂度为 O(n),空间复杂度为 O(n) 最坏情况,但如果 BST 平衡,时间和空间的平均情况为 O(logn)。
这是一个很受欢迎的面试问题,我能找到的关于该主题的唯一一篇文章来自 TopCoder .不幸的是,从面试答案的角度来看,它看起来过于复杂。 除了绘制到两个节点的路径并推导祖先之外,是否有更简单的方法来
我在面试中被问到这个问题,虽然我知道如何解决这个问题,但我没有回答。我不知道该怎么做的原因是因为问题是这样给我的: class Node{ int val; Node *lef
我正在尝试提出获取上下文项的所有祖先的解决方案。一种选择是将 _path 存储在索引中,另一种选择是执行类似于以下操作的操作: http://www.glass.lu/Blog/GettingAnce
我正在尝试用java实现n叉树中多个节点的LCA。我正在处理句子的解析树,因此假设节点的子节点数 <= 6 是合理的。这里的多个节点是句子中的两个短语(连续单词序列)。令 k 为涉及的节点数。 一种方
我是一名优秀的程序员,十分优秀!