尝试运行我的代码时,出现此“错误:在此包装器小部件上方找不到正确的提供程序”。我似乎无法弄清楚为什么它不读取提供程序。我只是想从Widget接收值。
我尝试将StreamProvider包装在MaterialApp内部。我也尝试过使用Builder窗口小部件发送上下文,但是似乎无法弄清楚问题所在。尝试获取“ Provider.of(context)
时出现问题void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: MaterialApp(
home: Wrapper(),
),
);
}
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
User _userFromDB(FirebaseUser user){
return user != null ? User(uid: user.uid) : null;
}
Stream<User> get user{
return _auth.onAuthStateChanged
.map(_userFromDB);
}
Future signInAnon() async {
try{
AuthResult result = await _auth.signInAnonymously();
FirebaseUser user = result.user;
return _userFromDB(user);
} catch(e){
print(e.toString());
return null;
}
}
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
print(context);
final user = Provider.of<User>(context);
print(user);
return Authenticate();
}
}
在此先感谢您的帮助!