パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.3.0

streams_channel

StreamsChannelはEventChannelにインスパイアされています。Flutterとプラットフォーム側の間でイベントのストリームを作成できるようにします。

30日間ダウンロード
169
いいね数
14
Pub ポイント
40

パッケージをインストール

flutter pub add streams_channel

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

StreamsChannel は EventChannel にインスパイアされた Dart パッケージで、Flutter とプラットフォーム側の間で双方向のストリーミングイベントを可能にします。Flutter がネイティブプラットフォーム(Android や iOS など)から継続的なデータストリームを受け取り、逆方向にも送信できるようにすることで、リアルタイム通信を実現します。プラットフォーム固有のコードとのシームレスな統合を目的としており、複雑なボイラープレートなしに非同期イベントフローを扱いやすくしています。軽量でストリームベースの相互作用に特化しており、ライブアップデート、センサーデータ、バックグラウンド通知などの用途に最適です。

用途

  • リアルタイムデータ同期
  • センサー データ ストリーミング
  • バックグラウンドタスクの通知
  • ライブイベント配信
  • プラットフォームからFlutterへの通信

主な機能

  • 双方向ストリームのサポート
  • イベントチャネル用のシンプルな API
  • 軽量で最小限の依存関係
  • ネイティブプラットフォームの統合
  • イベント駆動型アーキテクチャ

適している対象

  • リアルタイム機能を開発する開発者
  • プラットフォーム側のイベント処理が必要なアプリ
  • ネイティブプラグインを使用するプロジェクト
  • 非同期データフローと連携するチーム
  • ネイティブ統合を備えた Flutter アプリ

注意事項

  • プラットフォーム側の実装が必要です
  • ストリームベースの通信に限定
  • 組み込みのエラー処理なし
  • 単一値の応答には適していません
  • 両側での適切な設定に依存する

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

StreamsChannel for Flutter plugin development

pub package

StreamsChannel is inspired from EventChannel. It allows to create streams of events between Flutter and platform side.

Rationale

EventChannel allows to only create a single open stream per channel. On the first subscription, the stream will be open and it's possible to pass arguments from Flutter to platform side. Subsequent subscriptions will either re-use the open stream or override it with a new one.

In order to have multiple streams open at the same time, with different parameters for stream initialization, one has to create multiple EventChannel. And it gets complicated if the number of streams is dynamic.

Installation

Follow the instructions: https://pub.dev/packages/streams_channel#-installing-tab-

How it works

StreamsChannel works in a similar way than EventChannel. The difference is that it supports multiple open streams at the same time.

On the platform side, the channel takes a stream handler factory:

let channel = FlutterStreamsChannel(name: "my_channel", binaryMessenger: plugin.registrar.messenger())
channel.setStreamHandlerFactory { arguments in
  return MyHandler(arguments) // MyHandler is an instance FlutterStreamHandler
}

On the flutter side, each call to receiveBroadcastStream(args) will create a new stream, that won't replace any previous one that is still running.

  1. The first subscription to the stream will create a new stream handler using the factory on the platform side, then trigger onListen on the handler;
  2. The last subscription to the stream will trigger onCancel on the handler, then destroy the handler instance
  3. If a new first subscription happens afterwards, repeat step 1.

Under the hood, StreamsChannel tracks each stream with a simple auto-incremented unique id.

Example

Flutter side
final StreamsChannel channel = new StreamsChannel('some_channel');

// continuous stream of events from platform side, match some args
channel.receiveBroadcastStream('some args')
  .listen((data) {
    // do something
  });

// another continuous stream of events from platform side, matching some other args
channel.receiveBroadcastStream('some other args')
  .listen((data) {
    // do something
  });
Platform side
Android
public class DemoPlugin {
  public static void registerWith(PluginRegistry.Registrar registrar) {
    final StreamsChannel channel = new StreamsChannel(registrar.messenger(), "streams_channel_test");
    channel.setStreamHandlerFactory(new StreamsChannel.StreamHandlerFactory() {
      @Override
      public EventChannel.StreamHandler create(Object arguments) {
        return new StreamHandler();
      }
    });
  }

  // Send "Hello" 10 times, every second, then ends the stream
  public static class StreamHandler implements EventChannel.StreamHandler {

    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (count > 10) {
            eventSink.endOfStream();
        } else {
            eventSink.success("Hello " + count + "/10");
        }
        count++;
        handler.postDelayed(this, 1000);
      }
    };

    private EventChannel.EventSink eventSink;
    private int count = 1;

    @Override
    public void onListen(Object o, final EventChannel.EventSink eventSink) {
      this.eventSink = eventSink;
      runnable.run();
    }

    @Override
    public void onCancel(Object o) {
      handler.removeCallbacks(runnable);
    }
  }
}
iOS
@interface StreamHandler : NSObject<FlutterStreamHandler>
  @property(strong, nonatomic) NSTimer *timer;
  @property(assign, nonatomic) NSInteger count;
@end

@implementation DemoPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

  FlutterStreamsChannel *channel = [FlutterStreamsChannel streamsChannelWithName:@"streams_channel_test" binaryMessenger:registrar.messenger];
  [channel setStreamHandlerFactory:^NSObject<FlutterStreamHandler> *(id arguments) {
    return [StreamHandler new];
  }];
}

@end

// Send "Hello" 10 times, every second, then ends the stream
@implementation StreamHandler

- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events {
  self.count = 1;
  self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
    if(self.count > 10) {
      events(FlutterEndOfEventStream);
    } else {
      events([NSString stringWithFormat:@"Hello %ld/10", (long)self.count]);
      self.count++;
    }
  }];

  return nil;
}

- (FlutterError *)onCancelWithArguments:(id)arguments {
  [self.timer invalidate];
  self.timer = nil;
  return nil;
}
@end

Author

Beacons plugin is developed by Loup, a mobile development studio based in Montreal and Paris.
You can contact us at <hello@loup.app>

License

Apache License 2.0

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト