cloud_functions_ffi 0.1.1
cloud_functions_ffi: ^0.1.1 copied to clipboard
The cloud_functions implementation for desktop Linux, backed by the Firebase C++ SDK through firebase_ffi. Registers itself, so apps use cloud_functions unchanged.
// SPDX-FileCopyrightText: 2026 Joel Winarske
// SPDX-License-Identifier: Apache-2.0
// Calls a callable named echo.
//
// The only lines specific to Linux are the registerWith calls below. Every
// other line is ordinary cloud_functions and would compile unchanged on Android.
import 'package:cloud_functions/cloud_functions.dart';
import 'package:cloud_functions_ffi/cloud_functions_ffi.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_ffi/firebase_core_ffi.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// On Linux nothing registers these automatically yet, so an app says so
// once at startup. Elsewhere the plugin registers itself and these are not
// called.
FirebaseCoreFfi.registerWith();
CloudFunctionsFfi.registerWith();
await Firebase.initializeApp(
// Replace with your project's values, or generate them with the FlutterFire
// CLI. The C++ SDK reads them from here rather than from a plist.
options: const FirebaseOptions(
apiKey: 'replace-me',
appId: '1:1:android:1',
messagingSenderId: '1',
projectId: 'replace-me',
),
);
runApp(const _ExampleApp());
}
class _ExampleApp extends StatefulWidget {
const _ExampleApp();
@override
State<_ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<_ExampleApp> {
String _status = 'press the button';
Future<void> _run() async {
setState(() => _status = 'working...');
try {
final message = await _work();
setState(() => _status = message);
} on Object catch (e) {
// Shown rather than swallowed: a binding that cannot reach the SDK
// fails here, and the reason is the useful part.
setState(() => _status = 'failed: $e');
}
}
Future<String> _work() async {
final result = await FirebaseFunctions.instance.httpsCallable('echo').call({
'hi': 1,
});
return 'callable answered ${result.data}';
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('cloud_functions on Linux')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(24),
child: Text(_status, textAlign: TextAlign.center),
),
FilledButton(onPressed: _run, child: const Text('Run')),
],
),
),
),
);
}
}