我正在一个屏幕上对用户进行身份验证,然后将其推到另一个屏幕,但是在另一个屏幕上,该变量不可访问,其中包含我已通过变量传递的用户详细信息。 detailsUser
在Home()中不可访问;即使我提到过要推到另一页。它进行身份验证,不会带我到另一个屏幕。
class SignInScreen extends StatefulWidget {
@override
_SignInScreenState createState() => _SignInScreenState();
}
class _SignInScreenState extends State<SignInScreen> {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Future<FirebaseUser> _signIn(BuildContext context) async{
Fluttertoast.showToast(
msg: 'Signed In'
);
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleAuth.accessToken,
accessToken: googleAuth.idToken,
);
FirebaseUser userDetails = (await _firebaseAuth.signInWithCredential(credential)) as FirebaseUser;
ProviderDetails providerInfo = new ProviderDetails(userDetails.providerId);
List<ProviderDetails> providerData = List <ProviderDetails>();
providerData.add(providerInfo);
UserDetails details = UserDetails(
userDetails.providerId,
userDetails.displayName,
userDetails.photoUrl,
userDetails.email,
providerData);
Navigator.push(context, MaterialPageRoute(builder: (context) => ThemeConsumer(child: Home(detailsUser: details))));
return userDetails;
}
@override
Widget build(BuildContext context) {
return Scaffold();
class Home extends StatefulWidget {
final UserDetails detailsUser;
Home({Key key, @required this.detailsUser}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(
detailsUser.username //detailsUser is not accessible here, why?
)));
答案 0 :(得分:1)
要从该小部件的Widget
中访问State class
字段,可以使用widget.fieldName
(如Patel Pinkal所说)
这里有一个link,可以更熟悉Flutter中的小部件
答案 1 :(得分:1)
如Patel Pinkal在评论中所说:
Use this "widget.detailsUser.username"
代替
"detailsUser.username"