简体   繁体   English

如何在Flutter Web项目中设置Firebase功能

[英]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? 我正在尝试在Flutter Web项目中配置Firebase实时数据库,是否对我有任何建议在Flutter Web项目中配置Firebase实时数据库?

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? https://pub.dev/packages/firebase#-installing-tab-我已经按照上面的链接配置了firebase libraray,但是如何使用firebase实时数据库功能呢?

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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