To get the current user ID from Firebase in Flutter, you can use the FirebaseAuth
class and the currentUser
property. Here’s an example:
import 'package:firebase_auth/firebase_auth.dart';
FirebaseUser currentUser = await FirebaseAuth.instance.currentUser;
String userId = currentUser.uid;
This code imports the FirebaseAuth
class and calls the currentUser
property to get the current user. If the user is signed in, the code stores the user’s ID in the userId
variable. If the user is not signed in, currentUser
will be null
, and you should handle this case appropriately in your code.