0

I want to change some settings if i'm locally developing the app, such as:

settings.dart

bool debug = true;

String serverUrl;

if (debug) {
  serverUrl = 'http://localhost'; 
} else {
  serverUrl = 'https://myserver.com';
}

And then, use it with:

requests.dart

import 'settings.dart';

void fetchApi async () {
  response = http.get(serverUrl);

  // do some stuff
}

But actually, I can't use the if function outside a function, the error says:

Expected an identifier.dart(missing_identifier)

Is there something that I can do to achieve this?

Thanks.

Enzo Dtz
  • 361
  • 1
  • 16

1 Answers1

1

In your case, What you can do is define a getter for url as shown in this code:

String get serverUrl {
    If(kDebugMode) {
        return "https://myserver.com";
    } else {
        return "https://myserver.com";
    }
}
Kishan Dhankecha
  • 957
  • 7
  • 25