currentUser()不能正常工作,为什么我看到该表达式不能求值为函数,因此无法调用它 [英] currentUser() is not working type why I am seeing the expression doesn't evaluate to a function, so it can't be invoked
问题描述
我是Flutter的新手,注册时我正在开发一个简单的身份验证应用程序,我应该导航到聊天屏幕,并且我想抓住当前用户,但是当我调用FireBaseAuth.instance.currentUser()时,我得到了一个例外该表达式不求值一个函数,因此无法调用它."为什么我看到这个?请帮助
I'm new in Flutter, I'm working on a simple authentication app when I registered i should navigate to the chat screen and i want to grab current user but when i call FireBaseAuth.instance.currentUser() I got an exception "the expression doesn't evaluate to a function, so it can't be invoked." Why I'm seeing this? Please Help
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
class ChatScreen extends StatefulWidget {
static const String id = "chatScreen";
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final FirebaseAuth _auth = FirebaseAuth.instance;
// ignore: deprecated_member_use
FirebaseUser loginUser;
@override
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
// HERE I got an Exception "The expression doesn't evaluate to a function, so it can't be invoked."
final user = await _auth.currentUser();
if (user != null) {
loginUser = user;
print(loginUser.email);
}
}
推荐答案
为什么要调用 currentUser
作为函数?这是一个字段,而不是方法.
Why do you invoke currentUser
as a function ? It is a field, not a method.
final user = await _auth.currentUser;
更新:由于 currentUser
不会返回将来,因此即使未引起错误(在非未来的对象在封面下导致将对象包裹在新的未来中.
Update:
Since currentUser
does not return a future, the use of await
is useless, even if it doesn't cause errors (await
on a non-future object causes, under the cover, the object to be wrapped in a new future).
文档参考: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/currentUser.html
这篇关于currentUser()不能正常工作,为什么我看到该表达式不能求值为函数,因此无法调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!