【问题标题】:get the current user name and email in the user account drawer in the navigation drawer flutter fire store在导航抽屉flutter fire store的用户帐户抽屉中获取当前用户名和电子邮件
【发布时间】:2020-08-03 18:08:19
【问题描述】:

我使用 firestore 数据库制作了一个颤振应用程序,我需要在导航抽屉 UserAccountsDrawerHeader 中显示当前登录用户的电子邮件和姓名,并在尝试时显示一些问题

import 'package:flutter/material.dart';
import 'package:loginapp/services/auth.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:provider/provider.dart';
import 'package:loginapp/models/User.dart';

class CusDash extends StatefulWidget {
  @override
  _CusDashState createState() => _CusDashState();
}

class _CusDashState extends State<CusDash> {
  final AuthService _auth = AuthService();
  final databaseReference = Firestore.instance;
  final Firestore database = Firestore.instance;
  var firebaseUser = FirebaseAuth.instance.currentUser();

  readData() async {
    try {
      var firebaseUser = await FirebaseAuth.instance.currentUser();
      DocumentSnapshot snapshot = await databaseReference
          .collection('profile')
          .document(firebaseUser.uid)
          .get();
      // print(snapshot.data['userType']);
      // String result = snapshot.data['userType'];
      //print(result);

    } catch (e) {
      print(e.toString());
    }
  }

  void _signedOut() async {
    try {
      await _auth.signOut();
      //onSignedOut();
    } catch (e) {
      print(e);
    }
  }

  getData(DocumentSnapshot doc) {
    return UserAccountsDrawerHeader(
      accountName: Text('name: ${doc.data['name']}'),
      accountEmail: Text('name: ${doc.data['email']}'),
      currentAccountPicture: CircleAvatar(
        backgroundColor: Colors.white,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    return Scaffold(
      appBar: new AppBar(
        title: new Text("tryout"),
        actions: <Widget>[
          new FlatButton(
            onPressed: _signedOut,
            child: new Text('Logout',
                style: new TextStyle(fontSize: 17.0, color: Colors.white)),
          ),
        ],
      ),
      body: new Container(
        child: Text("Welcome to flutter"),
      ),
      drawer: Drawer(
        elevation: 15.0,
        child: Column(
          children: <Widget>[
//            readData(),
//            UserAccountsDrawerHeader(
//              accountName: Text('name: ${firebaseUser.['name']}'),
//              accountEmail: Text('name: ${firebaseUser.['email']}'),
//              currentAccountPicture: CircleAvatar(
//                backgroundColor: Colors.white,
//              ),
//            ),

            StreamBuilder<QuerySnapshot>(
              // stream: databaseReference.collection('profile').snapshots(),
              stream: readData(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Column(
                      children: snapshot.data.documents
                          .map<Widget>((doc) => getData(doc))
                          .toList());
                } else {
                  return SizedBox();
                }
              },
            ),

            ListTile(
              title: Text('All Inbox'),
              leading: Icon(Icons.mail),
            ),
            Divider(
              height: 0.2,
            ),
            ListTile(
              title: Text('Primary'),
              leading: Icon(Icons.inbox),
            ),
            Divider(
              height: 0.2,
            ),
            ListTile(
              title: Text('Social'),
              leading: Icon(Icons.people),
            ),
            Divider(
              height: 0.2,
            ),
            ListTile(
              title: Text('Promotion'),
              leading: Icon(Icons.local_offer),
            ),
            Divider(
              height: 0.2,
            ),
          ],
        ), 
      ),
    );
  }
}

这是我的完整代码,我收到一个错误,例如“未来”类型不是“流”类型的子类型

在 readData() 函数中,我获取了当前用户,但我无法在流生成器中使用该函数。 谁能帮我获取当前用户的电子邮件和姓名?

【问题讨论】:

标签: flutter google-cloud-firestore navigation-drawer user-accounts userid


【解决方案1】:

我使用此代码获取文档快照

Future getUserProfile() async {
  DocumentSnapshot document = await FirebaseFirestore.instance
      .collection('Users')
      .doc(FirebaseAuth.instance.currentUser.email)
      .get();
  return document;
}

在主类中声明这些变量来存储数据

String storeName;
String storePhoneNo;
String storeGender;
String storeAddress;

使用 Future Builder 获取数据

FutureBuilder(
        future: getUserProfile(),
        builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting)
          return SpinKitDoubleBounce(color: kPrimaryColor,);
          storeName = snapshot.data['Name'];
          storePhoneNo = snapshot.data['Phone Number'];
          storeGender = snapshot.data['Gender'];
          storeAddress = snapshot.data['Address'];
return Column(
childern[
Text(storeName),
Text(storePhoneNo ),
Text(storeGender ),
Text(storeAddress ),
]
)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多