'Import variables from another .dart file to .dart file in flutter

My globals.dart file in same directory:


var apiKey= "AAAAXeuC-XXXXXXXXXXXXXXXXXXXXXXXXXX0rKLb2CWty0MHUzaZ";
var appId= "1:40338f8d8e";
var messagingSenderId= "40338";
var projectId="XXXXXXXXXXXXX";
var databaseURL= "XXXXXXXXXXXX";

I want to import these variables to main.dart file in android studio. How I did??

import 'globals.dart' as globals;

Imported correctly, no errors How I used??

apiKey: globals.apiKey

Its inside,

  if(Firebase.apps.isNotEmpty){
    await Firebase.initializeApp(
      name: "HCchatbot",
      options: const FirebaseOptions(
          apiKey: ,
          appId: ,
          messagingSenderId:,
          projectId: ,
          databaseURL:"
      ),
    );
  }

The error:

lib/main.dart:12:27: Error: Not a constant expression.
          apiKey: globals.apiKey,
                          ^^^^^^
lib/main.dart:11:22: Error: Constant evaluation error:
      options: const FirebaseOptions(
                     ^
lib/main.dart:12:27: Context: The invocation of 'apiKey' is not allowed in a constant expression.
          apiKey: globals.apiKey,
                          ^

Can somebody help me out?? Thanks in advance



Solution 1:[1]

all of your values in globals.dart should be turned to const values =>

const String apiKey= "AAAAXeuC-XXXXXXXXXXXXXXXXXXXXXXXXXX0rKLb2CWty0MHUzaZ";
const String appId= "1:40338f8d8e";
const String messagingSenderId= "40338";
const String projectId="XXXXXXXXXXXXX";
const String databaseURL= "XXXXXXXXXXXX";

try again and it should work.

another solution is removing the const in the FirebaseOptions

options: FirebaseOptions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kami Tzayig