When are static members evaluated in Dart?

Viewed 26

I'm trying to load the api url from my .env file (I'm using the flutter_dotenv package) and store it on a static member apiUrl of an abstract class named Constants.

The problem is that apiUrl gets evaluated before I can load the environment variables from my main function, which throws the error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Instance of 'NotInitializedError'

The main function

void main() async {

  await dotenv.load();

  runApp(const App());
}

The Constants class

abstract class Constants {
  static final String apiUrl = dotenv.get('API_URL');
  // other stuff
}
  

Is there a way to solve this? Thanks in advance for any responses

0 Answers
Related