问题
I have been using Firebase for authentication of users in the app. FirebaseAuth.getInstance().getCurrentUser().getMetadata()
returns null
when user comes back to the app after the sign up. It works perfectly when user signs up for the first time, but returns null
for returning users.
回答1:
Malcolm from the Firebase Team here.
Thanks for pointing this out. This is presently a bug in the persistence done by the Android SDK. We'll get on fixing this ASAP - keep an eye on upcoming release notes to know when it's resolved.
In the meantime, the workaround you can use is to write to your app's SharedPreferences to store those attributes at sign up time, and read from those Prefs when you need them elsewhere in your app. Hope this helps!
回答2:
Ran into the same issue with getMetadata() returning null.
I had added Firebase support in Android Studio using the Firebase Assistant wizard. I just could not get FirebaseUser.getMetadata() to work.
Then I updated the build.gradle (Module: app) dependencies as follows:
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.1'
(These were the latest versions at the time of this writing. By the time you read this, there may be even newer versions)
I found that I still got null but then I went to add a new user and all of a sudden it started working. I had deleted all existing accounts and created new ones - don't know if that made a difference. But it seems to be working now.
So - updating the dependencies and doing a gradle sync may fix your problem, at least for new users.
The Firebase Assistant in Android Studio probably needs to be modified to reflect the latest versions.
(Still worried about about this being completely reliable..)
来源:https://stackoverflow.com/questions/48079683/firebase-user-returns-null-metadata-for-already-signed-up-users