搜尋套件、儲存庫與 APP 分類

套件詳情v0.8.0

dbus

D-Bus 訊息總線用戶端的原生 Dart 實作。此套件允許 Dart 應用程式直接存取 Linux 桌面服務。

WindowsLinux
近 30 天下載
854萬
喜歡數
80
Pub 分數
160

安裝套件

flutter pub add dbus

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

D-Bus 訊息總線客戶端的原生 Dart 實現,讓 Dart 應用程式能夠直接與 Linux 桌面服務通訊。此套件提供對 D-Bus 的底層存取,允許與會話管理、硬體事件和桌面整合等系統及應用服務互動。此庫專為在 Linux 上建構桌面應用程式的開發者設計,並透過相容的 D-Bus 實作支援 Windows 和 macOS。該函式庫提供乾淨、原生 Dart API,用於透過 D-Bus 發送和接收訊息。

適用情境

  • 與 Linux 系統服務互動
  • 在Dart應用中整合桌面功能
  • 訪問硬體和會話事件
  • 建構跨平台桌面工具
  • 啟用與其他桌面應用程式之間的通訊

主要特色

  • 原生 Dart D-Bus 客戶端
  • 直接存取 Linux 桌面服務
  • 對訊息總線(會話和系統)的支援
  • 跨平台相容性(Linux、Windows、macOS)
  • 乾淨的非同步 Dart API

適合對象

  • 針對 Linux 的桌面應用程式開發者
  • 系統級整合
  • 使用 Dart 開發桌面工具的開發者
  • 需要桌面服務通訊的應用程式
  • 需要低階層 Linux 結合的專案

使用建議

  • 主要針對 Linux 環境設計
  • 除了基本用法之外,文件內容有限
  • 目標系統上需要 D-Bus 守護程序可用
  • 在 Windows/macOS 上可能需要平台特定的設定
  • 不適用於行動裝置或Web平台

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Pub Package codecov

A native Dart client implementation of D-Bus.

Accessing remote objects

The easiest way to get started is to generate Dart classes from a D-Bus interface definition, for example:

<node name="/com/example/Test/Object">
  <interface name="com.example.Test">
    <property name="Version" type="s" access="read"/>
    <method name="ReverseText">
      <arg name="input" type="s" direction="in"/>
      <arg name="output" type="s" direction="out"/>
    </method>
  </interface>
</node>

The dart-dbus tool processes this interface to generate a Dart source file:

$ dart-dbus generate-remote-object test-object.xml -o test-remote-object.dart

You can then use the generated test-remote-object.dart to access that remote object from your program:

import 'package:dbus/dbus.dart';
import 'test-remote-object.dart';

Future<void> main() async {
  var client = DBusClient.session();
  var object = ComExampleTestObject(client, 'com.example.Test');
  var version = await object.getVersion();
  print('version: $version');
  var reversedText = await object.callReverseText('Hello World');
  print(reversedText);
  await client.close();
}

The code generated by dart-dbus may not be the cleanest API you want to expose for your service. It is recommended that you use the generated code as a starting point and then modify it as necessary.

Exporting local objects

The above interface definition can also be processed to generate a class for exporting this object.

$ dart-dbus generate-object test-object.xml -o test-object.dart

You can then use the generated test-object.dart in your program:

import 'package:dbus/dbus.dart';
import 'test-object.dart';

class TestObject extends ComExampleTestObject {
  @override
  Future<DBusMethodResponse> getVersion() async {
    return DBusGetPropertyResponse(DBusString('1.2'));
  }

  @override
  Future<DBusMethodResponse> doReverseText(String input) async {
    var reversedText = String.fromCharCodes(input.codeUnits.reversed);
    return DBusMethodSuccessResponse([DBusString(reversedText)]);
  }
}

Future<void> main() async {
  var client = DBusClient.session();
  await client.requestName('com.example.Test');
  await client.registerObject(TestObject());
}

Using objects without dbus-dart

You can also choose to access D-Bus objects directly without interface definitions and dbus-dart. This requires writing more code and handing more error cases.

The following code shows how to access the remote object in the above examples:

import 'package:dbus/dbus.dart';

Future<void> main() async {
  var client = DBusClient.session();
  var object = DBusRemoteObject(client, name: 'com.example.Test', path: DBusObjectPath('/com/example/Test/Object'));
  var value = await object.getProperty('com.example.Test', 'Version', signature: DBusSignature('s'));
  var version = value.asString();
  print('version: $version');
  var response = await object.callMethod('com.example.Test', 'ReverseText', [DBusString('Hello World')], replySignature: DBusSignature('s'));
  var reversedText = response.values[0].asString();
  print('$reversedText');
  await client.close();
}

And the following shows how to export that object:

import 'package:dbus/dbus.dart';

class TestObject extends DBusObject {
  TestObject() : super(DBusObjectPath('/com/example/Test/Object'));

  @override
  Future<DBusMethodResponse> getProperty(String interface, String name) async {
    if (interface == 'com.example.Test' && name == 'Version') {
      return DBusGetPropertyResponse(DBusString('1.2'));
    } else {
      return DBusMethodErrorResponse.unknownProperty();
    }
  }

  @override
  Future<DBusMethodResponse> handleMethodCall(DBusMethodCall methodCall) async {
    if (methodCall.interface == 'com.example.Test') {
      if (methodCall.name == 'ReverseText') {
        if (methodCall.signature != DBusSignature('s')) {
          return DBusMethodErrorResponse.invalidArgs();
        }
	var input = methodCall.values[0].asString();
        var reversedText = String.fromCharCodes(input.codeUnits.reversed);
        return DBusMethodSuccessResponse([DBusString(reversedText)]);
      } else {
        return DBusMethodErrorResponse.unknownMethod();
      }
    } else {
      return DBusMethodErrorResponse.unknownInterface();
    }
  }
}

Future<void> main() async {
  var client = DBusClient.session();
  await client.requestName('com.example.Test');
  await client.registerObject(TestObject());
}

Contributing to dbus.dart

We welcome contributions! See the contribution guide for more details.

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案