颤抖地从statefulwidget访问局部变量

时间:2020-03-24 02:05:27

标签: firebase flutter dart firebase-authentication navigation-drawer

到目前为止,我正在构建一个具有登录页面和主页的应用程序。我可以用google登录,并且可以重定向到当前页面(首页)。我有一个显示名称和一个电子邮件地址,它是登录页面的输出。我想使用这些详细信息显示在我的抽屉中。有没有一种方法可以从登录页面访问这些变量并在主页上使用它们。

这是我的Google登录功能。

Future  <String> signInWithGoogle() async {
  final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;
  final AuthCredential credential = GoogleAuthProvider.getCredential(accessToken: googleSignInAuthentication.accessToken, idToken: googleSignInAuthentication.idToken,);
  final AuthResult authResult = await _auth.signInWithCredential(credential);
  final FirebaseUser user = authResult.user;

  assert(!user.isAnonymous);
  assert(await user.getIdToken() != null);

  final FirebaseUser currentUser = await _auth.currentUser();
  assert(user.uid == currentUser.uid);
  print("signed in " + user.displayName);
  print("signed in " + user.email);
  return 'signInWithGoogle succeeded: $user';
}

我使用Gmail登录并获得了输出。

I/flutter (15019): signed in Shero thehero
I/flutter (15019): signed in aakashsrh@gmail.com

这是我的主页代码。

class Homepage extends StatefulWidget {

  TextEditingController _searchController =TextEditingController();
  Future signOutGoogle() async{
    await googleSignIn.signOut();
    print("User Sign Out");
  }

  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  TextEditingController _searchController =TextEditingController();
  @override
  void initState() {
    super.initState();
      }

在应用程序中运行以下代码时,输​​出为NULL。

new UserAccountsDrawerHeader(
                accountName: Text("${user?.displayName}"),
                accountEmail: Text('user?.email'),
                currentAccountPicture: GestureDetector(
                  child:new CircleAvatar(
                    backgroundColor: Colors.blueGrey,
                    child: Icon(Icons.person,color: Colors.white,),
                  ),
                ),

here is the output for the code in my drawer

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/cv" style="@style/CardView.Light" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" tools:ignore="ContentDescription"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/mcqDescriptionTextView" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:text="mcqDescriptionTextView" android:textColor="#3F51B5" android:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <RadioGroup android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/mcqDescriptionTextView" app:layout_constraintVertical_bias="0.0"> <RadioButton android:id="@+id/option1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" tools:layout_editor_absoluteX="61dp" tools:layout_editor_absoluteY="99dp" /> <RadioButton android:id="@+id/option2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" tools:layout_editor_absoluteX="61dp" tools:layout_editor_absoluteY="99dp" /> <RadioButton android:id="@+id/option3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" tools:layout_editor_absoluteX="61dp" tools:layout_editor_absoluteY="99dp" /> <RadioButton android:id="@+id/option4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" tools:layout_editor_absoluteX="61dp" tools:layout_editor_absoluteY="99dp" /> </RadioGroup> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> 应该在呼叫user = authResult.user

所以:

setState()

在没有{/ {1}}更改的情况下,小部件不知道/何时更改,所以不知道何时重新绘制。