无法解析最终的 FirebaseUser firebaseUser = FirebaseAuth.getInstance();

huangapple go评论82阅读模式
英文:

Cannot resolve final FirebaseUser firebaseUser = FirebaseAuth.getInstance();

问题

无法解决:

final FirebaseUser firebaseUser = FirebaseAuth.getInstance();

如果我将其更改为:

final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

它将无法解决这里的 getUid

if (!user.getUser_id().equals(firebaseUser.getUid())){
                        mUsers.add(user);

如果我将其更改为 FirebaseUser firebaseUser = FirebaseUser.getInstance();,它将无法解决 getInstance();我该怎么做才能消除这个错误,请帮忙。

private void readUsers() {
    final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mUsers.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                User user = snapshot.getValue(User.class);

                assert user !=null;
                assert firebaseUser !=null;
                if (!user.getUser_id().equals(firebaseUser.getUid())){
                    mUsers.add(user);
                }
            }
            userAdapter = new UserAdapter(getContext(), mUsers);
            recyclerView.setAdapter(userAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
英文:

Cannot reolve

final FirebaseUser firebaseUser = FirebaseAuth.getInstance();

If I change it to

final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); 

it won't resolve the getUid in this

if (!user.getUser_id().equals(firebaseUser.getUid())){
                    mUsers.add(user);

And if I change it to FirebaseUser firebaseUser = FirebaseUser.getInstance(); it wont resolve the getInstance(); What can I do to remove the error please help

    private void readUsers() {
        final FirebaseUser firebaseUser = FirebaseAuth.getInstance();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mUsers.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    User user = snapshot.getValue(User.class);

                    assert user !=null;
                    assert firebaseUser !=null;
                    if (!user.getUser_id().equals(firebaseUser.getUid())){
                        mUsers.add(user);
                    }
                }
                userAdapter = new UserAdapter(getContext(), mUsers);
                recyclerView.setAdapter(userAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

答案1

得分: 1

FirebaseAuth.getInstance() 返回一个 FirebaseAuth 实例,因此这段代码:

final FirebaseUser firebaseUser = FirebaseAuth.getInstance();

应该改成这样:

final FirebaseAuth auth = FirebaseAuth.getInstance();

然后将这段代码:

assert user != null;
assert firebaseUser != null;
if (!user.getUser_id().equals(firebaseUser.getUid())) {
     mUsers.add(user);
}

改成这样:

if (!user.getUser_id().equals(auth.getCurrentUser().getUid())) {
     mUsers.add(user);
}
英文:

FirebaseAuth.getInstance() returns an instance of FirebaseAuth, so this:

final FirebaseUser firebaseUser = FirebaseAuth.getInstance();

should be this instead:

final FirebaseAuth auth = FirebaseAuth.getInstance();

Then change this:

assert user !=null;
assert firebaseUser !=null;
if (!user.getUser_id().equals(firebaseUser.getUid())){
     mUsers.add(user);
 }

into this:

if (!user.getUser_id().equals(auth.getCurrentUser().getUid())){
     mUsers.add(user);
}

huangapple
  • 本文由 发表于 2020年10月13日 17:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64332384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定