📜  firebase core flutter pub (1)

📅  最后修改于: 2023-12-03 15:15:04.468000             🧑  作者: Mango

Firebase Core Flutter Pub

Firebase Core Flutter Pub is a package that provides the core functionality of Firebase to your Flutter app. Firebase is a mobile and web application development platform that provides many features to make it easy to develop high-quality applications. With Firebase Core Flutter Pub, you can easily configure and initialize Firebase for your Flutter app, as well as use features like analytics and messaging.

Installation

To install Firebase Core Flutter Pub, add it to your project's pubspec.yaml file:

dependencies:
  firebase_core: ^1.7.0

Then run flutter pub get to download and install the package.

Configuration

To configure Firebase for your Flutter app, you need to create a Firebase project in the Firebase Console and add your app to that project. Once you've created your project and added your app, you can follow the instructions below to configure Firebase Core Flutter Pub.

Android

For Android, you need to download the google-services.json file from the Firebase Console and add it to your app's android/app directory. Then add the following to your app's android/app/build.gradle file:

apply plugin: 'com.google.gms.google-services'
iOS

For iOS, you need to download the GoogleService-Info.plist file from the Firebase Console and add it to your app's ios/Runner directory. Then add the following to your app's ios/Podfile file:

pod 'Firebase/Core'
Initialization

To initialize Firebase Core Flutter Pub in your Flutter app, you need to call the Firebase.initializeApp() method. This should be done before using any Firebase features in your app. Here's an example:

import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
Usage

Once you've configured and initialized Firebase Core Flutter Pub in your Flutter app, you can use its features. Here are some examples:

Analytics

To use Firebase Analytics in your app, you need to call the await FirebaseAnalytics.instance.logEvent(name: 'test_event'); method. This will log an event to Firebase Analytics. Here's an example:

import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      navigatorObservers: [
        FirebaseAnalyticsObserver(analytics: FirebaseAnalytics()),
      ],
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('My Home Page')),
      body: Center(
        child: ElevatedButton(
          child: Text('Log Event'),
          onPressed: () {
            FirebaseAnalytics.instance.logEvent(name: 'test_event');
          },
        ),
      ),
    );
  }
}
Messaging

To use Firebase Cloud Messaging (FCM) in your app, you need to configure FCM in the Firebase Console and add the necessary dependencies to your app. Then you can use the firebase_messaging package to receive and handle FCM messages. Here's an example:

import 'package:firebase_messaging/firebase_messaging.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.instance.getToken().then((value) => print(value));
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    print("onMessage: $message");
  });
  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    print("onMessageOpenedApp: $message");
  });
  runApp(MyApp());
}
Conclusion

Firebase Core Flutter Pub is an essential package for any Flutter developer who wants to use Firebase in their app. With its easy configuration and initialization, as well as its support for features like analytics and messaging, it makes integrating Firebase into your app a breeze.