Xamarin.Forms中Firebase实时数据库司机车辆信息展示与更新问题
Hey Patrick! Let's get this sorted out—you're right that pulling all drivers' data isn't what you want, and the issue comes down to how you're targeting the specific driver's node in Firebase.
The Core Problem with Your Current Code
Your original attempt used auth.CurrentUser directly as a child node name, but auth.CurrentUser is a full user object, not the string UID that Firebase uses to identify user-specific nodes. That's why you saw errors. Instead, you need to use the unique UID of the logged-in user, which is stored in auth.CurrentUser.Uid.
Step-by-Step Fix
First, make sure your Firebase Realtime Database structure is set up correctly: you should be storing each driver's vehicle info under Driver Vehicules/{userUid}, where {userUid} is the UID from Firebase Auth. This is the standard way to tie user data to their auth account.
1. Modify the Data Fetch Method
Instead of fetching all nodes under Driver Vehicules, we'll target only the node matching the logged-in user's UID. We'll also use OnceSingleAsync since we're expecting just one driver's data, not a list:
FirebaseAuth auth = FirebaseAuth.DefaultInstance; public async Task<CarAndDriverDisplayInfos> GetCurrentDriverInfos() { // First, check if a user is actually logged in if (auth.CurrentUser == null) { // Handle unauthenticated state (return null, show error, etc.) return null; } // Fetch the single driver record tied to the logged-in user's UID var driverData = await firebase .Child("Driver Vehicules") .Child(auth.CurrentUser.Uid) // Use the user's unique UID here .OnceSingleAsync<CarAndDriverDisplayInfos>(); if (driverData != null) { // Map the fetched data to your display model (if needed) return new CarAndDriverDisplayInfos { FNameDriver = driverData.FNameDriver, LNameDriver = driverData.LNameDriver, CTypeDriver = driverData.CTypeDriver, CModelDriver = driverData.CModelDriver, YCarDriver = driverData.YCarDriver, ImageCarUrl = driverData.ImageCarUrl, ImageDriverUrl = driverData.ImageDriverUrl, CarIdDriver = driverData.CarIdDriver, PersonId = driverData.PersonId }; } // Return null if no data exists for this user return null; }
2. Update the UI Binding
Since we're now fetching a single driver instead of a list, adjust your OnAppearing method to handle this and bind it to your list view:
protected async override void OnAppearing() { base.OnAppearing(); var currentDriver = await firebaseHelper.GetCurrentDriverInfos(); if (currentDriver != null) { // Wrap the single object in a list to match your ItemsSource requirement lstPersons.ItemsSource = new List<CarAndDriverDisplayInfos> { currentDriver }; } else { // Handle cases where no driver data exists or user isn't logged in lstPersons.ItemsSource = new List<CarAndDriverDisplayInfos>(); // Optional: Show a message to the user like "No driver info found" } }
3. Bonus: Ensure Data is Saved Correctly
To make this work end-to-end, when saving a driver's vehicle info, use the same UID as the node name:
public async Task SaveOrUpdateDriverInfo(CarAndDriverDisplayInfos driverInfo) { if (auth.CurrentUser == null) return; // Save data to Driver Vehicules/{userUid} await firebase .Child("Driver Vehicules") .Child(auth.CurrentUser.Uid) .PutAsync(driverInfo); }
Why This Works
By using the user's UID as the node key, you create a direct, secure link between the authenticated user and their data. This not only lets you fetch the correct record easily but also helps with security rules—you can set up Firebase rules to ensure users can only read/write their own data.
内容的提问来源于stack exchange,提问作者Patrick Mokem

