【发布时间】:2021-11-15 10:27:49
【问题描述】:
class MessageScreen extends StatefulWidget {
@override
_MessageScreenState createState() => _MessageScreenState();
}
class _MessageScreenState extends State<MessageScreen> {
var fanMessage = '';
var userDocument;
var userId;
@override
void initState() {
super.initState();
_getId();
}
TextEditingController messageController = TextEditingController();
void _messageSubmit() {
Firestore.instance
.collection('messages/ne6MUaXi2wup9WOcZq85/alerts')
.add({'text': messageController.text});
}
Future _getId() async {
var test = await FirebaseAuth.instance.currentUser();
setState(() {
userId = test.uid;
print(userId);
Firestore.instance
.collection('users')
.document(userId)
.get()
.then((DocumentSnapshot documentSnapshot) {
userDocument = documentSnapshot.data['role'];
});
});
}
// Widget _getId() {
// return FutureBuilder(future: Firestore.instance.collection('users'),child: Text());
// }
@override
Widget build(BuildContext context) {
print('User ID: ${userId}');
print('User Role: ${userDocument}');
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(60.0),
child: AppBar(
title: Text('Fan Page'),
actions: [
DropdownButton(
icon: Icon(
Icons.more_vert,
color: Theme.of(context).primaryIconTheme.color,
),
items: [
DropdownMenuItem(
child: Container(
child: Row(
children: <Widget>[
Icon(Icons.exit_to_app),
SizedBox(
width: 8,
),
Text('Logout')
],
),
),
value: 'logout')
],
onChanged: (itemIdentifier) {
if (itemIdentifier == 'logout') {
FirebaseAuth.instance.signOut();
}
},
)
],
),
),
body: StreamBuilder(
stream: Firestore.instance
.collection('messages/ne6MUaXi2wup9WOcZq85/alerts')
.snapshots(),
builder: (ctx, streamSnapshot) {
if (streamSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
final documents = streamSnapshot.data.documents;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(10),
child: Card(
elevation: 10,
// decoration:
// BoxDecoration(border: Border.all(color: Colors.black)),
child: Padding(
padding: const EdgeInsets.fromLTRB(5.0, 20.0, 5.0, 20.0),
child: Container(
child: Text(
documents[index]['text'],
),
),
),
),
),
);
},
),
floatingActionButton: Visibility(
visible: checkIfAdmin(),
child: Center(
child: Container(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 0, 70),
child: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) =>
_buildPopupDialog(context),
);
},
),
),
),
),
));
}
bool checkIfAdmin() {
// getData();
// print(userDocument['role']);
if (userDocument == 'admin') {
return true;
} else {
return false;
}
}
好的,我想要做的是,只为“管理员”用户角色显示浮动操作按钮。逻辑似乎在起作用,因为当我在启动应用程序后热重载一次时会发生这种情况。当我启动应用程序并登录时,我会看到一个红色窗口,并显示“无效参数”的错误消息,当我在不更改任何内容的情况下热重新加载时,我得到了我的屏幕,然后它就可以正常工作了。为什么会这样?我还没有完全理解状态概念。所以请帮忙!
编辑:我已经停止收到红屏错误消息。相反,应用程序启动。但是根据我的检查。
我的问题是我的应用程序只有在热重载后才能正常工作。只有在热重载管理员看到floatingActionButton。
在热重载之前的调试控制台中,我收到此错误:
未处理的异常:NoSuchMethodError:方法“[]”在 null 上被调用。
【问题讨论】:
标签: firebase flutter google-cloud-firestore firebase-authentication