Flutter Firebase Auth:如何在Flutter抽屉中显示用户电子邮件和名称

时间:2019-04-24 22:10:39

标签: firebase flutter firebase-authentication

我已经从Firebase Auth中成功检索了用户电子邮件和姓名。但是我想在Flutter Drawer中显示它

姓名和电子邮件检索:

    Future<FirebaseUser> _handleSignIn() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  final FirebaseUser user = await _auth.signInWithCredential(credential);
  print("signed in " + user.displayName);
  print("signed in " + user.email);
  return user;
}

颤动抽屉:

drawer: new Drawer(
    elevation: 10.0,
    child: new ListView(
      children: <Widget>[
        new UserAccountsDrawerHeader(
          accountName: new Text("Name"), //I want to display user NAME here
          accountEmail: new Text("Email"), //display user EMAIL here 
          decoration: new BoxDecoration(
              image: new DecorationImage(
                  fit: BoxFit.fill,
                  image: new NetworkImage(
                      "https://img00.deviantart.net/35f0/i/2015/018/2/6/low_poly_landscape__the_river_cut_by_bv_designs-d8eib00.jpg"))),
        ),

1 个答案:

答案 0 :(得分:1)

只需调用当前用户,然后您将拥有currentUser数据,可在抽屉中使用它

// call this in initState for example
final FirebaseUser user = await _auth.currentUser();

// reset of code
drawer: Drawer(
  elevation: 10.0,
  child: ListView(
    children: <Widget>[
      UserAccountsDrawerHeader(
        accountName: Text("${user.displayName}"),
        accountEmail: Text("${user.email}"),
        decoration: BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: NetworkImage("${user.photoUrl}"),
          ),
        ),
      ),
    ],
  ),
),

整页示例

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class ExamplePage extends StatefulWidget {
  @override
  _ExamplePageState createState() => _ExamplePageState();
}

class _ExamplePageState extends State<ExamplePage> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  FirebaseUser user;

  @override
  void initState() {
    super.initState();
    initUser();
  }

  initUser() async {
    user = await _auth.currentUser();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("this is an appbar yaay!"),
      ),
      drawer: Drawer(
        elevation: 10.0,
        child: ListView(
          children: <Widget>[
            UserAccountsDrawerHeader(
              accountName: Text("${user?.displayName}"),
              accountEmail: Text("${user?.email}"),
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: NetworkImage("${user?.photoUrl}"),
                ),
              ),
            ),
          ],
        ),
      ),
      body: Center(
        child: Container(),
      ),
    );
  }
}