如何解决:Future Provider 6.0.2 找不到正确的提供者 Flutter
我尝试在我的应用程序中使用 Future Provider,但当时将其注册在我的小部件树的顶部,如下所示:
import 'package:flutter/material.dart';
import 'package:flutter_application_9_future_provider/function.dart'; //here is my future class
import 'package:flutter_application_9_future_provider/homescreen.dart'; //here is the screen
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
Busqueda bus = Busqueda(); // my class is called "Busqueda" and has a method (buscarrequest)that returns a future
@override
Widget build(BuildContext context) {
return FutureProvider(
initialData: 'vacio',
create: (context) => bus.buscarrequest(),
child: const MaterialApp(
home: Home(),
),
);
}
}
我收到错误消息:
小部件库捕获异常═══════════════════════════════════ 构建 Home(dirty) 时抛出以下 ProviderNotFoundException: 错误:找不到正确的提供商
在此主页小部件上方 发生这种情况是因为您使用了不包含提供程序的
BuildContext
由您选择。有一些常见的场景:...
使用 ChangeNotifier Provider 这不会发生,为什么? 错误消息中有一些示例,但我尝试了但不起作用:
例如,而不是:
小部件构建(BuildContext上下文){ 返回提供者<示例>( 创建:(_) =>例子(), 会抛出 ProviderNotFoundError,因为 `context` 是关联的 到作为“Provider
”父级的小部件 子:文本(context.watch<示例>()), ), } 考虑使用
builder
,如下所示:小部件构建(BuildContext上下文){ 返回提供者<示例>( 创建:(_) =>例子(), 我们使用“builder”来获取可以访问提供者的新“BuildContext” 构建器:(上下文){ 不再投掷 返回文本(context.watch<示例>()), } ), }
建议如何解决此错误。 提前致谢!
在flutter应用程序中使用未来的提供程序(提供程序:^6.0.2)没有错误。
I try to use Future Provider in my app but at the time to register it on the top of my widget tree like so:
import 'package:flutter/material.dart';
import 'package:flutter_application_9_future_provider/function.dart'; //here is my future class
import 'package:flutter_application_9_future_provider/homescreen.dart'; //here is the screen
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
Busqueda bus = Busqueda(); // my class is called "Busqueda" and has a method (buscarrequest)that returns a future
@override
Widget build(BuildContext context) {
return FutureProvider(
initialData: 'vacio',
create: (context) => bus.buscarrequest(),
child: const MaterialApp(
home: Home(),
),
);
}
}
I get and error that says:
Exception caught by widgets library ═══════════════════════════════════
The following ProviderNotFoundException was thrown building Home(dirty):
Error: Could not find the correct Provider<Busqueda> above this Home WidgetThis happens because you used a
BuildContext
that does not include the provider
of your choice. There are a few common scenarios:...
Using ChangeNotifier Provider this do not happen, why?
There is some example in the error message but I tried it but do not work:
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>()),
}
),
}
Please advice in how to solve this error.
Thanks in advance!
Use future provider (provider: ^6.0.2) in flutter app with no error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论