|
| 1 | +// ignore_for_file: require_trailing_commas, public_member_api_docs, library_private_types_in_public_api |
| 2 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license that can be |
| 4 | +// found in the LICENSE file. |
| 5 | + |
| 6 | +import 'dart:core'; |
| 7 | + |
| 8 | +import 'package:cloud_functions/cloud_functions.dart'; |
| 9 | +import 'package:firebase_core/firebase_core.dart'; |
| 10 | +import 'package:flutter/material.dart'; |
| 11 | + |
| 12 | +FirebaseOptions get firebaseOptions => const FirebaseOptions( |
| 13 | + apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0', |
| 14 | + authDomain: 'react-native-firebase-testing.firebaseapp.com', |
| 15 | + databaseURL: 'https://react-native-firebase-testing.firebaseio.com', |
| 16 | + projectId: 'react-native-firebase-testing', |
| 17 | + storageBucket: 'react-native-firebase-testing.appspot.com', |
| 18 | + messagingSenderId: '448618578101', |
| 19 | + appId: '1:448618578101:web:0b650370bb29e29cac3efc', |
| 20 | + measurementId: 'G-F79DJ0VFGS', |
| 21 | + ); |
| 22 | + |
| 23 | +late FirebaseFunctions functions; |
| 24 | + |
| 25 | +Future<void> main() async { |
| 26 | + WidgetsFlutterBinding.ensureInitialized(); |
| 27 | + await Firebase.initializeApp(options: firebaseOptions); |
| 28 | + functions = FirebaseFunctions.instance |
| 29 | + ..useFunctionsEmulator('localhost', 5001); |
| 30 | + runApp(MyApp()); |
| 31 | +} |
| 32 | + |
| 33 | +class MyApp extends StatefulWidget { |
| 34 | + MyApp({Key? key}) : super(key: key); |
| 35 | + @override |
| 36 | + _MyAppState createState() => _MyAppState(); |
| 37 | +} |
| 38 | + |
| 39 | +class _MyAppState extends State<MyApp> { |
| 40 | + List fruit = []; |
| 41 | + |
| 42 | + @override |
| 43 | + void initState() { |
| 44 | + super.initState(); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + Widget build(BuildContext context) { |
| 49 | + return MaterialApp( |
| 50 | + home: Scaffold( |
| 51 | + appBar: AppBar( |
| 52 | + title: const Text('Firebase Functions Example'), |
| 53 | + ), |
| 54 | + body: Center( |
| 55 | + child: ListView.builder( |
| 56 | + itemCount: fruit.length, |
| 57 | + itemBuilder: (context, index) { |
| 58 | + return ListTile( |
| 59 | + title: Text('${fruit[index]}'), |
| 60 | + ); |
| 61 | + })), |
| 62 | + floatingActionButton: Builder( |
| 63 | + builder: (context) => FloatingActionButton.extended( |
| 64 | + onPressed: () async { |
| 65 | + // See index.js in the functions folder for the example function we |
| 66 | + // are using for this example |
| 67 | + final callable = functions.httpsCallable('listFruit', |
| 68 | + options: HttpsCallableOptions( |
| 69 | + timeout: const Duration(seconds: 5))); |
| 70 | + |
| 71 | + await callable().then((v) { |
| 72 | + setState(() { |
| 73 | + fruit.clear(); |
| 74 | + // ignore: avoid_dynamic_calls |
| 75 | + v.data.forEach((f) { |
| 76 | + fruit.add(f); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }).catchError((e) { |
| 80 | + ScaffoldMessenger.of(context).showSnackBar(SnackBar( |
| 81 | + content: Text('ERROR: $e'), |
| 82 | + )); |
| 83 | + }); |
| 84 | + }, |
| 85 | + label: const Text('Call Function'), |
| 86 | + icon: const Icon(Icons.cloud), |
| 87 | + backgroundColor: Colors.deepOrange, |
| 88 | + ), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments