I am using Plugin.Firebase (v2.0.0) and i am using the below firebase recommended code in Maui.App.CreateBuilder and getting errors in Lambda expressions:
CS1503 Errors are:
Argument 2: cannot convert from Android.OS.Bundle to Plugin.Firebase.Bundled.Shared.CrossFirebaseSettings
Argument 1: cannot convert from UIKit.UIApplication to Plugin.Firebase.Bundled.Shared.CrossFirebaseSettings
Argument 2: cannot convert from Foundation.NSDictionary to Firebase.Core.Options
Tried using casts, but that does work for these types. Any suggestions?
using Plugin.Firebase.Auth;
#if IOS
using Plugin.Firebase.Bundled.Platforms.iOS;
#elif ANDROID
using Plugin.Firebase.Bundled.Platforms.Android;
#endif
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCompatibility()
.UseMauiCommunityToolkit()
.ConfigureSyncfusionCore()
.RegisterFirebaseServices()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
return builder.Build();
}
private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
{
builder.ConfigureLifecycleEvents(events => {
#if IOS
events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
CrossFirebase.Initialize(app, launchOptions);
return false;
}));
#else
events.AddAndroid(android => android.OnCreate((activity,state) =>
CrossFirebase.Initialize(activity, state)));
#endif
});
builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current);
return builder;
}
}
3
Answers
First of all, you’ll need to make sure that you’re only targeting iOS and Android, because the Plugin doesn’t support MacCatalyst and Windows, so you’ll need to update the target frameworks in your project file or add conditional package includes:
Then, you’ll need to make sure that you’ve also placed preprocessor directives around the
using ...
statements in the file where you also have theRegisterFirebaseServices()
method:Depending on the .NET version you’re on, you may need to change the
net6.0-ios
andnet6.0-android
targets tonet7.0-ios
andnet7.0-android
(or higher, for future readers).That’s all more or less described in the documentation, as well.
The setup instructions in the documentation had a mistake, the
CrossFirebase.Initialize
method doesn’t require any arguments on iOS and only anActivity
on android, so your code should actually look like this:The mistake in the readme got fixed accordingly.
From the sample code MauiProgram.cs, we can find that the code of function
RegisterFirebaseServices
has usedCreateCrossFirebaseSettings
And the code of
CrossFirebaseSettings
just as follows:For more information, you can check the sample code here.