简体   繁体   中英

How to set up firebase function in flutter web project

I am trying to configure the firebase real-time database in my flutter web project is there any suggestion for me to config the firebase real-time database in flutter web project?

https://pub.dev/packages/firebase#-installing-tab- I have followed the above link to config the firebase libraray but how can I use the firebase real-time data base function?

You can use a helper class to init your database

import 'package:firebase/firebase.dart' as fb;

class FirebaseHelper {
  static fb.Database initDatabase() {
    try {
      if (fb.apps.isEmpty) {
        fb.initializeApp(
          apiKey: "add_your_own",
          authDomain: "add_your_own",
          databaseURL: "add_your_own",
          projectId: "add_your_own",
          messagingSenderId: "add_your_own",
        );
      }
    } on fb.FirebaseJsNotLoadedException catch (e) {
      print(e);
    }
    return fb.database();
  }
}

Then use it as follow

// global variable, not the best 
Database database = FirebaseHelper.initDatabase();

void main() {
  runApp(FirebaseApp());
}

Then in your widget use

 var databaseRef = database.ref("my_database").child("my_data");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM