-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmain.dart
More file actions
93 lines (83 loc) · 2.99 KB
/
main.dart
File metadata and controls
93 lines (83 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ignore_for_file: require_trailing_commas, public_member_api_docs, library_private_types_in_public_api
// 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 'package:cloud_functions/cloud_functions.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
FirebaseOptions get firebaseOptions => const FirebaseOptions(
apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0',
authDomain: 'react-native-firebase-testing.firebaseapp.com',
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
projectId: 'react-native-firebase-testing',
storageBucket: 'react-native-firebase-testing.appspot.com',
messagingSenderId: '448618578101',
appId: '1:448618578101:web:0b650370bb29e29cac3efc',
measurementId: 'G-F79DJ0VFGS',
);
late FirebaseFunctions functions;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: firebaseOptions);
functions = 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
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
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) => FloatingActionButton.extended(
onPressed: () async {
// See index.js in the functions folder for the example function we
// are using for this example
final callable = functions.httpsCallable('listFruit',
options: HttpsCallableOptions(
timeout: const Duration(seconds: 5)));
await callable().then((v) {
setState(() {
fruit.clear();
// ignore: avoid_dynamic_calls
v.data.forEach((f) {
fruit.add(f);
});
});
}).catchError((e) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('ERROR: $e'),
));
});
},
label: const Text('Call Function'),
icon: const Icon(Icons.cloud),
backgroundColor: Colors.deepOrange,
),
),
),
);
}
}