Error: Could not find the correct Provider<AppStateNotifier> above this Consumer<AppStateNotifier> Widget
Error: Could not find the correct Provider<AppStateNotifier> above this Consumer<AppStateNotifier> Widget
我尝试将提供者功能添加到我的应用程序后出现以下错误:
Error: Could not find the correct Provider above this Consumer Widget
这是我的 ChangeNotifier 代码:
import 'package:flutter/material.dart';
class AppStateNotifier extends ChangeNotifier {
bool isDarkMode = false;
void updateTheme(bool isDarkMode) {
this.isDarkMode = isDarkMode;
notifyListeners();
}
}
还有我的主要 class:
import 'package:wilson/src/config/theme_data.dart';
import 'package:wilson/src/routes/index.dart';
import 'package:wilson/src/utils/app_state_notifier.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Wilson extends StatelessWidget {
const Wilson({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Consumer<AppStateNotifier>(builder: (context, appState, child) {
return MaterialApp(
title: 'App-Name',
theme: ThemeConfig.lightTheme,
darkTheme: ThemeConfig.darkTheme,
themeMode: appState.isDarkMode ? ThemeMode.dark : ThemeMode.light,
onGenerateRoute: routes,
);
});
}
}
也许有人可以帮助我,因为我相信这只是一个愚蠢的小错误。
您必须像这样将其初始化到您的 main.dart 中。
如果您有多个提供程序文件,您可以使用多提供程序。
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: Doctorss()),
ChangeNotifierProvider.value(value: Categoriess()),
ChangeNotifierProvider.value(value: TopDoctors()),
ChangeNotifierProvider.value(value: Appointments()),
ChangeNotifierProvider.value(value: TopArticlesProvider()),
],
child: MaterialApp(
theme: ThemeData(
appBarTheme: AppBarTheme(color: Colors.blue[800]),
primaryColor: Colors.blue[800],
colorScheme:
ColorScheme.fromSwatch().copyWith(secondary: Colors.orange)),
debugShowCheckedModeBanner: false,
home: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapShot) {
if (snapShot.connectionState == ConnectionState.waiting) {
return const Start();
}
if (snapShot.hasData) {
return const homestate();
}
return const AuthScreen();
}),
这是我的main.dart编码
import 'package:flutter_user_profile/home/wrapper.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_user_profile/services/auth.dart';
import 'package:provider/provider.dart';
import 'package:flutter_user_profile/model/user.dart';
Future <void> main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: FirebaseOptions(apiKey: "AIzaSyD59Nz0y4Z8S-rVpeu5E5lslsW_8WYrEiE",
appId: "XXX", messagingSenderId: "XXX", projectId: "parkingtech-f1449") );
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context){
return StreamProvider<Client?>.value(
initialData: null,
value: AuthService().user,
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Wrapper(),
)
);
}
}
我不确定如何添加另一个 return,因为我的 main.dart
上已经有 StreamProvider
我尝试将提供者功能添加到我的应用程序后出现以下错误:
Error: Could not find the correct Provider above this Consumer Widget
这是我的 ChangeNotifier 代码:
import 'package:flutter/material.dart';
class AppStateNotifier extends ChangeNotifier {
bool isDarkMode = false;
void updateTheme(bool isDarkMode) {
this.isDarkMode = isDarkMode;
notifyListeners();
}
}
还有我的主要 class:
import 'package:wilson/src/config/theme_data.dart';
import 'package:wilson/src/routes/index.dart';
import 'package:wilson/src/utils/app_state_notifier.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Wilson extends StatelessWidget {
const Wilson({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return Consumer<AppStateNotifier>(builder: (context, appState, child) {
return MaterialApp(
title: 'App-Name',
theme: ThemeConfig.lightTheme,
darkTheme: ThemeConfig.darkTheme,
themeMode: appState.isDarkMode ? ThemeMode.dark : ThemeMode.light,
onGenerateRoute: routes,
);
});
}
}
也许有人可以帮助我,因为我相信这只是一个愚蠢的小错误。
您必须像这样将其初始化到您的 main.dart 中。 如果您有多个提供程序文件,您可以使用多提供程序。
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: Doctorss()),
ChangeNotifierProvider.value(value: Categoriess()),
ChangeNotifierProvider.value(value: TopDoctors()),
ChangeNotifierProvider.value(value: Appointments()),
ChangeNotifierProvider.value(value: TopArticlesProvider()),
],
child: MaterialApp(
theme: ThemeData(
appBarTheme: AppBarTheme(color: Colors.blue[800]),
primaryColor: Colors.blue[800],
colorScheme:
ColorScheme.fromSwatch().copyWith(secondary: Colors.orange)),
debugShowCheckedModeBanner: false,
home: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapShot) {
if (snapShot.connectionState == ConnectionState.waiting) {
return const Start();
}
if (snapShot.hasData) {
return const homestate();
}
return const AuthScreen();
}),
这是我的main.dart编码
import 'package:flutter_user_profile/home/wrapper.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_user_profile/services/auth.dart';
import 'package:provider/provider.dart';
import 'package:flutter_user_profile/model/user.dart';
Future <void> main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: FirebaseOptions(apiKey: "AIzaSyD59Nz0y4Z8S-rVpeu5E5lslsW_8WYrEiE",
appId: "XXX", messagingSenderId: "XXX", projectId: "parkingtech-f1449") );
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context){
return StreamProvider<Client?>.value(
initialData: null,
value: AuthService().user,
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: Wrapper(),
)
);
}
}
我不确定如何添加另一个 return,因为我的 main.dart
上已经有 StreamProvider