搜索分类、插件与代码仓

插件详情v1.0.3

firebase_database_rest

一个基于 REST 的跨平台 Dart/Flutter 包装器,用于 Firebase 实时数据库 API。

近 30 天下载
38
喜欢数
18
Pub 分数
140

安装插件

flutter pub add firebase_database_rest

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个与平台无关的 Dart/Flutter 包,为 Firebase 实时数据库提供基于 REST 的封装。它通过 HTTP 请求实现与 Firebase 数据库的无缝交互,适用于无法使用原生 Firebase SDK 的环境。该包将底层 REST 调用抽象为直观的 Dart 方法,支持数据读取、写入以及通过轮询实现的实时更新。设计简洁,跨平台兼容性好,非常适合需要 Firebase 集成但不依赖平台特定插件的应用。

适合场景

  • 跨平台 Firebase 数据库访问
  • 服务器端Flutter应用程序
  • Web 和桌面应用程序集成
  • 离线优先场景下的手动同步
  • 轻量级 Firebase 交互

主要特点

  • 基于REST API的通信
  • 平台无关的设计
  • 简单的 CRUD 操作
  • 基于轮询的实时更新
  • 无原生 SDK 依赖项

适合对象

  • 面向网络或桌面的开发人员
  • 避免使用原生 Firebase 插件的项目
  • 使用Flutter的后端服务
  • 需要轻量级 Firebase 访问的 App

使用建议

  • 手动处理实时更新
  • 比原生 SDK 的延迟更高
  • 仅限 REST 兼容功能
  • 无内置离线持久化
  • 需要手动处理错误和重试逻辑

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

firebase_database_rest

Continous Integration Pub Version

A platform independent Dart/Flutter Wrapper for the Firebase Realtime Database API based on REST

Features

  • Pure Dart-based implementation
    • works on all platforms supported by dart
  • Uses the official REST-API endpoints
  • Provides high-level classes to manage database access
  • Supports Model-Binding via Generics and converter functions
  • Full support of all Realtime database features:
    • Supports CRUD
    • Supports Queries (filtering, sorting)
    • Supports realtime server updates (streaming)
    • Supports ETags
  • Provides a Transaction class to wrap ETag-Logic into safe transactions
  • Provides timestamp class for server set timestamps
  • Proivides low-level REST classes for direct API access (import firebase_database_rest/rest.dart)
  • Fully integrated with firebase_auth_rest - including automatic auth token refreshes

Installation

Simply add firebase_database_rest to your pubspec.yaml and run pub get (or flutter pub get).

Usage

The libraries primary class is the FirebaseStore. It is a high-level class that allows access to a certain part of the realtime database using Model binding. With this store, you specify a subpatch and can then perform various operations on elements below that path (list key, create, read, write or delete them, ...). This class also provides the APIs for streaming changes and transactions. Typically, your application would use multiple stores, one for each dataclass you plan to store at a certain path.

The second important class is the FirebaseDatabase. It serves as the toplevel class, parent to all stores and manages the database connection itself, including the management of the associated FirebaseAccount (from firebase_auth_rest) and background ressources (like the REST-API interface).

The following code is a simple example, which can be found in full length, including errorhandling, at https://pub.dev/packages/firebase_database_rest/example. It loggs into firebase as anonymous user, starts streaming changes and then does some db operations.

// The data class that is stored in firebase
class ExampleModel {
  final int id;
  final String data;

  const ExampleModel(this.id, this.data);
  
  // ...
}

// obtain a firebase account, using firebase_auth_rest
final account = // ...

// create a database reference from that account
final database = FirebaseDatabase(
  account: account,
  database: 'my-firebase-realtime-database-name,
  basePath: 'application/${account.localId}/example',
);

// create typed store. In this example, a callback store is used, but you can
// also just extend FirebaseStore
final store = database.createRootStore<ExampleModel>(
  onDataFromJson: (dynamic json) => ExampleModel.fromJson(json),
  onDataToJson: (data) => data.toJson(),
  onPatchData: (data, updatedFields) => data.patch(updatedFields),
);

// add a stream listener that reports database changes in realtime
sub = (await store.streamAll()).listen((e) => print('Stream update: $e'));

// add some data to the store
await store.create(const ExampleModel(1, 'A'));

// get all data in store
print('All data: ${await store.all()}');

// cleanup: stop streaming and dispose of the database
await sub.cancel();
await database.dispose();

Documentation

The documentation is available at https://pub.dev/documentation/firebase_database_rest/latest/. A full example can be found at https://pub.dev/packages/firebase_database_rest/example.

所属分类

依赖

依赖清单 6 项

源码仓库

使用此插件的项目

暂无相关数据。