Kirti Purohit
Kirti Purohit

Reputation: 89

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

Upvotes: 0

Views: 448

Answers (1)

Kami Tzayig
Kami Tzayig

Reputation: 354

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

Upvotes: 1

Related Questions