flutter_cpp_bridge 1.0.1 copy "flutter_cpp_bridge: ^1.0.1" to clipboard
flutter_cpp_bridge: ^1.0.1 copied to clipboard

PlatformLinux

A Flutter package that simplifies calling C++ code from Dart via FFI. Provides Service, ServicePool, and StandaloneService abstractions around dart:ffi to manage C++ shared-library lifecycle and messa [...]

example/lib/main.dart

import 'package:ffi/ffi.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cpp_bridge/service_pool.dart';
import 'libalone.dart';
import 'libaservice.dart';
import 'libbservice.dart';
import 'libcservice.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    final ValueNotifier<int> color = ValueNotifier<int>(0x0000FFFF);
    final ValueNotifier<String> text = ValueNotifier<String>("abcde");
    final ValueNotifier<int> count = ValueNotifier<int>(0);

    // Create services pool
    var servicePool = ServicePool();

    // Example of how to create a service
    var libaService = LibAService("liba.so");
    var libbService = LibBService("libb.so");

    // Example of standalone service (no-op start/stop)
    var libAlone = AloneService("libalone.so");

    // Example of standalone service with real start/stop (FCB_EXPORT_STANDALONE).
    // start_service() resets the counter; stop_service() clears it.
    var libC = LibCService("libc.so");

    // Binding service to frontend.
    // No nullptr check needed: assignJob callbacks are only invoked for real
    // messages — the stream never emits nullptr since switching to NativeCallable.
    libaService.assignJob((message) {
      color.value = libaService.getHexaColor(message);
      libAlone.hello();
    });

    libbService.assignJob((message) {
      final cPtr = libbService.getText(message);
      text.value = cPtr.toDartString();
    });

    servicePool.addService(libaService);
    servicePool.addService(libbService);

    // No startPolling() needed — delivery is event-driven via NativeCallable.

    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ValueListenableBuilder<int>(
                valueListenable: color,
                builder: (context, color, child) {
                  return Text(
                    'Hello World!',
                    style: TextStyle(
                      color: Color(color),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  );
                },
              ),

              ValueListenableBuilder<String>(
                valueListenable: text,
                builder: (context, text, child) {
                  return Text(
                    text,
                    style: TextStyle(
                      color: Color(0xFF000000),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  );
                },
              ),

              const SizedBox(height: 24),
              ValueListenableBuilder<int>(
                valueListenable: count,
                builder: (context, count, child) {
                  return Text(
                    'Count: $count',
                    style: const TextStyle(fontSize: 20),
                  );
                },
              ),
              ElevatedButton(
                onPressed: () => count.value = libC.increment(),
                child: const Text('Increment (libc.so)'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
150
points
117
downloads

Publisher

verified publisherrenaud-barrau.fr

Weekly Downloads

A Flutter package that simplifies calling C++ code from Dart via FFI. Provides Service, ServicePool, and StandaloneService abstractions around dart:ffi to manage C++ shared-library lifecycle and message polling.

Repository (GitHub)
View/report issues

Topics

#ffi #native #linux #plugin

Documentation

API reference

License

MIT (license)

Dependencies

ffi, flutter, plugin_platform_interface

More

Packages that depend on flutter_cpp_bridge

Packages that implement flutter_cpp_bridge