Search code examples
flutterenvironment-variablesfacebook-authentication

How to use env variable before runApp(MyApp());


I need to pass an env variable to be used like this

import 'package:flutter/foundation.dart' show kIsWeb;  
.
.
.
void main() {
  // check if is running on Web
  if (kIsWeb) {
  final appId = String.fromEnvironment('FACEBOOK_APP_ID',
  defaultValue: 'somedefaultvalue');
    // initialiaze the facebook javascript SDK
    FacebookAuth.instance.webInitialize(
      appId: appId,//<-- YOUR APP_ID
      cookie: true,
      xfbml: true,
      version: "v9.0",
    );
  }
  runApp(MyApp());
}

I get an error

string.fromenvironment can only be used as a constant constructor

is there a way to do this?

Thank you


Solution

  • Would you change from 'final' to 'const?

    import 'package:flutter/foundation.dart' show kIsWeb;  
    .
    .
    .
    void main() {
      // check if is running on Web
      if (kIsWeb) {
      const appId = String.fromEnvironment('FACEBOOK_APP_ID',
      defaultValue: 'somedefaultvalue');
        // initialiaze the facebook javascript SDK
        FacebookAuth.instance.webInitialize(
          appId: appId,//<-- YOUR APP_ID
          cookie: true,
          xfbml: true,
          version: "v9.0",
        );
      }
      runApp(MyApp());
    }