A Flutter Plugin Allowing You to Use Firebase Cloud Functions

Firebase Cloud Functions for Flutter

A Flutter plugin to use the Cloud Functions for Firebase API.

To learn more about Functions, please visit the Firebase website

Getting Started

To get started with Cloud Functions for Firebase, please see the documentation.

Usage

To use this plugin, please visit the Functions Usage documentation

Issues and feedback

Please file FlutterFire specific issues, bugs, or feature requests in our issue tracker.

Plugin issues that are not specific to FlutterFire can be filed in the Flutter issue tracker.

To contribute a change to this plugin, please review our contribution guide and open a pull request.

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add cloud_functions

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  cloud_functions: ^4.3.4

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:cloud_functions/cloud_functions.dart';

example/lib/main.dart

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:core';
import 'dart:io';

import 'package:cloud_functions/cloud_functions.dart';
import 'package:cloud_functions_example/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  // You should have the Functions Emulator running locally to use it
  // https://firebase.google.com/docs/functions/local-emulator
  FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);

  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List fruit = [];

  @override
  Widget build(BuildContext context) {
    final localhostMapped =
        kIsWeb || !Platform.isAndroid ? 'localhost' : '10.0.2.2';

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Firebase Functions Example'),
        ),
        body: Center(
          child: ListView.builder(
            itemCount: fruit.length,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text('${fruit[index]}'),
              );
            },
          ),
        ),
        floatingActionButton: Builder(
          builder: (context) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                FloatingActionButton.extended(
                  onPressed: () async {
                    // See .github/workflows/scripts/functions/src/index.ts for the example function we
                    // are using for this example
                    HttpsCallable callable =
                        FirebaseFunctions.instance.httpsCallable(
                      'listFruit',
                      options: HttpsCallableOptions(
                        timeout: const Duration(seconds: 5),
                      ),
                    );

                    await callingFunction(callable, context);
                  },
                  label: const Text('Call Function'),
                  icon: const Icon(Icons.cloud),
                  backgroundColor: Colors.deepOrange,
                ),
                const SizedBox(height: 10),
                FloatingActionButton.extended(
                  onPressed: () async {
                    // See .github/workflows/scripts/functions/src/index.ts for the example function we
                    // are using for this example
                    HttpsCallable callable =
                        FirebaseFunctions.instance.httpsCallableFromUrl(
                      'http://$localhostMapped:5001/flutterfire-e2e-tests/us-central1/listfruits2ndgen',
                      options: HttpsCallableOptions(
                        timeout: const Duration(seconds: 5),
                      ),
                    );

                    await callingFunction(callable, context);
                  },
                  label: const Text('Call 2nd Gen Function'),
                  icon: const Icon(Icons.cloud),
                  backgroundColor: Colors.deepOrange,
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  Future<void> callingFunction(
    HttpsCallable callable,
    BuildContext context,
  ) async {
    try {
      final result = await callable();
      setState(() {
        fruit.clear();
        result.data.forEach((f) {
          fruit.add(f);
        });
      });
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('ERROR: $e'),
        ),
      );
    }
  }
}

Download details:

Author: firebase.google.com

Source: https://github.com/firebase/flutterfire/tree/master/packages/cloud_functions/cloud_functions

#flutter #android #ios #web-development #google #firebase #api #cloud 

A Flutter Plugin Allowing You to Use Firebase Cloud Functions
1.10 GEEK