我正在关注YouTube教程,以使用Flutter构建应用。由于某种原因,我收到此错误。我找不到针对此问题的任何修复程序。
///在构建Wrapper(dirty)时引发了以下ProviderNotFoundError: 错误:在此包装小部件上方找不到正确的提供程序
要解决,请:
context
。这是我的main.dart代码
void main() => runApp(Gradebook());
class Gradebook extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: MaterialApp(
home: Wrapper(),
),
);
}
Wrapper.dart
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
if (user == null){
return Authenticate();
} else{
return Home();
}
}
}
在此处创建流提供程序。
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
// create user obj based on firebase user
User _userFromFirebaseUser(FirebaseUser user) {
return user != null ? User(uid: user.uid) : null;
}
//auth change user stream
Stream<User> get user {
return _auth.onAuthStateChanged
.map((FirebaseUser user) => _userFromFirebaseUser(user));
}
}
这是Authenticate.dart
class Authenticate extends StatefulWidget {
@override
_AuthenticateState createState() => _AuthenticateState();
}
class _AuthenticateState extends State<Authenticate> {
bool showLogin = true;
void toggleView() {
setState(() => showLogin = !showLogin);
}
@override
Widget build(BuildContext context) {
if(showLogin){
return Login(toggleView: toggleView);
} else{
return Register(toggleView: toggleView);
}
}
}