简体   繁体   中英

flutter get user email

i want to get my flutter login form user id into a text box.i used firebase to make authentication. i wrote a method to get user email.

  _getUserEmail(){

    FutureBuilder<FirebaseUser>(
      future: FirebaseAuth.instance.currentUser(),
      builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return new Text(snapshot.data.email);
        }
        else {
          return new Text('Loading...');
        }
      },
    );
  }

i want to get this user email into my flutter textbox view initial value. This is my flutter textbox.

ListTile(
       leading: Icon(Icons.info),
       title: TextFormField(
        decoration: InputDecoration(
              labelText: 'Enter Your name'
         ),
          initialValue: _getUserEmail(),
           onSaved: (val) => item.name = val,
           validator: (val) => val == "" ? val : null,
         ),
        ),

i want to get my email into ListTile initial value. can anyone help me?

Perhaps not a direct answer to your question. But using a form with validation for async authentication requires particular management of the validators to handle the async result. I but together a demo of how to manage the validators. The demo simulates the async call, so you would have to implement the firebase auth for your particular app. Might be a useful reference if still having problems.

在此处输入图片说明

You can find source code here

Edit: I created an example showing how to preload the form by inserting a widget that makes an async call. 在此处输入图片说明

I also got it to work using a FutureBuilder too, but this seemed cleaner. There are other ways to do it too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM