【发布时间】: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() 函数中,我获取了当前用户,但我无法在流生成器中使用该函数。 谁能帮我获取当前用户的电子邮件和姓名?
【问题讨论】:
-
这能回答你的问题吗? What is a Future and how do I use it?
标签: flutter google-cloud-firestore navigation-drawer user-accounts userid