Karol Wiśniewski
Karol Wiśniewski

Reputation: 518

How to set and use in code base URL link using Flavors?

I am implementing flavors in my app. I set them like this:

flavorDimensions "default"
    productFlavors {
        production {
            dimension "default"
            buildConfigField "string", "BASE_URL", "https://example.dev.com;"
        }
        dev {
            dimension "default"
            buildConfigField "string", "BASE_URL", "https://example.prod.com"
            versionNameSuffix "-dev"
        }
    }

and I want to access BASE_URL in my code, but I don't really know how and I am struggling to find answer.

I tried to use BuildConfig.BASE_URL but I can't use it as IDE saying it's undefined name.

It is a way to access BASE_URL directly in code or should I create some function which will be setting BASE_URL in code depending on actual flavor?

Upvotes: 1

Views: 338

Answers (1)

Keith DC
Keith DC

Reputation: 673

I'm unsure of the way you're injecting the BASE_URL, but perhaps this can read it? It works when passing in with --dart-define, and should also as args/toolArgs (unsure which) in the launch config.

const baseUrl = bool.hasEnvironment("BASE_URL")
    ? String.fromEnvironment("BASE_URL")
    : "https://example.dev.com";

Source: https://api.flutter.dev/flutter/dart-core/String/String.fromEnvironment.html

Upvotes: 0

Related Questions