f-miyu / Plugin.CloudFirestore

MIT License
121 stars 44 forks source link

"Failed to get document because the client is offline" error in Xamarin.Forms project #114

Open CostasAthan opened 9 months ago

CostasAthan commented 9 months ago

Here is my code of the App.xaml.cs file of a Xamarin.Forms project:

public partial class App : Application
    {
            public App()
            {
                CrossFirebaseCrashlytics.Current.HandleUncaughtException();

                InitializeComponent();

                MainPage = new Page();

                MainThread.BeginInvokeOnMainThread(async () => await Loader());
            }

            private async Task Loader()
            {
                var firebaseDocExists = false;

                var firebaseDoc = await CrossCloudFirestore.Current
                                                           .Instance
                                                           .Collection("myCollection")
                                                           .Document("docId")
                                                           .GetAsync();

                firebaseDocExists = firebaseDoc.Exists;

                if (firebaseDocExists == false) MainPage = new MyPage();
                else MainPage = new NavigationPage(new MainPage());
            }
    }

At the launch of the Android app on physical devices (API levels 29 & 31), not on the emulator, I get the following error:

crc64[alphanumeric_string].CrashlyticsException: Plugin.CloudFirestore.CloudFirestoreException: Failed to get document because the client is offline. at MyApp.App.Loader ()(:0) at MyApp.App.<.ctor>b__0_0 ()(:0)

Before the first Firestore reference in my initial code, I added this new line of code:

await CrossCloudFirestore.Current.Instance.EnableNetworkAsync();

No difference at all.

I also should add that running the app on the emulator does not produce the error, regardless which API level I use.

Environment details