问题
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);
}
}
回答1:
Why do you invoke currentUser
as a function ? It is a field, not a method.
final user = await _auth.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).
Doc reference: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/currentUser.html
回答2:
final currentUser = await _firebaseAuth.currentUser();
Simply replace the above code with
final currentUser = _firebaseAuth.currentUser;
来源:https://stackoverflow.com/questions/63667623/currentuser-is-not-working-type-why-i-am-seeing-the-expression-doesnt-evaluat