currentUser() is not working type why I am seeing the expression doesn't evaluate to a function, so it can't be invoked

岁酱吖の 提交于 2021-02-16 20:25:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!