xEnvironment variables

circle-check

To set environment variables go to PlutoFramework/appsettings.json file (if the file does not exist, create it) and fill in the contents like this:

{
    "VARIABLE_NAME_1": "value 1",
    "VARIABLE_NAME_2": "value 2"
}

These environment variables then need to be loaded in PlutoFramework/MauiProgram.cs using the AddAppSettings method. Add it to your code:

public static MauiAppBuilder AddAppSettings(this MauiAppBuilder builder)
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
    using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{nameof(YourAppNamespace)}.appsettings.json");
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.

    if (stream is null)
    {
        return builder;
    }

    IConfigurationRoot configuration = new ConfigurationBuilder()
        .AddJsonStream(stream)
        .Build();

    builder.Configuration.AddConfiguration(configuration);

    return builder;
}

You can then access the environment variable like this:

Last updated