为什么我在项目中收到提供程序错误?
Why im getting provider error in project?
当我尝试在我的代码中使用提供程序时出现此错误。
新的 flutter 和初学者所以也许任何人都可以解释如何修复我已经搜索但仍然找不到东西。
The following ProviderNotFoundException was thrown building MeinAccount(dirty, state: _MeinAccountState#e2cbb):
Error: Could not find the correct Provider<User> above this MeinAccount 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 your `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 that MeinAccount is under your MultiProvider/Provider<User>.
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(),
// 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>()),
}
),
}
If none of these solutions work, consider asking for help on Whosebug:
https://whosebug.com/questions/tagged/flutter
The relevant error-causing widget was:
MeinAccount file:///Users/myname/StudioProjects/projectname/lib/main.dart:48:41
When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:332:7)
#1 Provider.of (package:provider/src/provider.dart:284:30)
#2 _MeinAccountState.build (package:projectname/seitenleiste/meinacount.dart:362:27)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4704:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4587:15)
这是我的小部件 UPDATEEE:
Widget _buildPasswordTF() {
final user = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: DatbaseService(uid:user.uid).userData,
builder: (context, snapshot) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Password',
也许任何人都可以提供帮助:
这也是我的主要飞镖更新:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context){
return MultiProvider(
providers: [
//DatbaseService(create: (_) => User()), // add your providers like this.
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title:'Appbar Scaffold',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
primaryColor: Colors.white,
如果您需要更多信息,请添加评论。再次感谢。
这是因为您还没有注册您的供应商。在使用它们之前,您需要注册它们。使用 MultiProvider 包装您的 MaterialApp 小部件(如果您的应用程序中有多个提供程序)。
示例:
child: MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => User()), // add your providers like this.
],
child:MaterialApp(..........)
);
当我尝试在我的代码中使用提供程序时出现此错误。 新的 flutter 和初学者所以也许任何人都可以解释如何修复我已经搜索但仍然找不到东西。
The following ProviderNotFoundException was thrown building MeinAccount(dirty, state: _MeinAccountState#e2cbb):
Error: Could not find the correct Provider<User> above this MeinAccount 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 your `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 that MeinAccount is under your MultiProvider/Provider<User>.
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(),
// 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>()),
}
),
}
If none of these solutions work, consider asking for help on Whosebug:
https://whosebug.com/questions/tagged/flutter
The relevant error-causing widget was:
MeinAccount file:///Users/myname/StudioProjects/projectname/lib/main.dart:48:41
When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:332:7)
#1 Provider.of (package:provider/src/provider.dart:284:30)
#2 _MeinAccountState.build (package:projectname/seitenleiste/meinacount.dart:362:27)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4704:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4587:15)
这是我的小部件 UPDATEEE:
Widget _buildPasswordTF() {
final user = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: DatbaseService(uid:user.uid).userData,
builder: (context, snapshot) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Password',
也许任何人都可以提供帮助:
这也是我的主要飞镖更新:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context){
return MultiProvider(
providers: [
//DatbaseService(create: (_) => User()), // add your providers like this.
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title:'Appbar Scaffold',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
primaryColor: Colors.white,
如果您需要更多信息,请添加评论。再次感谢。
这是因为您还没有注册您的供应商。在使用它们之前,您需要注册它们。使用 MultiProvider 包装您的 MaterialApp 小部件(如果您的应用程序中有多个提供程序)。
示例:
child: MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => User()), // add your providers like this.
],
child:MaterialApp(..........)
);