1.. Deploy official function from https://flutter-firebase-docs.web.app/docs/functions/usage/#calling-endpoints
const functions = require('firebase-functions');
exports.listFruit = functions.https.onCall((data, context) => {
return ["Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"]
});
pubspec.yaml cloud_functions: ^0.7.0+1
firebase_core: ^0.5.2
// Copyright 2018, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:cloud_functions/cloud_functions.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseFunctions.instance
.useFunctionsEmulator(origin: 'http://localhost:5001');
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<dynamic> listOfFruits;
String exceptionMessage = '';
@override
Widget build(BuildContext context) {
final HttpsCallable callable = FirebaseFunctions.instance.httpsCallable(
'listFruit',
options: HttpsCallableOptions(timeout: const Duration(seconds: 30)));
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Cloud Functions example app'),
),
body: Center(
child: Container(
margin: const EdgeInsets.only(top: 32.0, left: 16.0, right: 16.0),
child: Column(
children: [
Text(listOfFruits.toString() ?? exceptionMessage),
MaterialButton(
child: const Text('Get Fruits'),
onPressed: () async {
try {
final HttpsCallableResult result = await callable.call();
print(result.data);
listOfFruits = [];
setState(() {
for (var item in result.data) {
listOfFruits.add(item);
}
});
} on FirebaseFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
setState(() {
listOfFruits = null;
exceptionMessage =
"caught firebase functions exception\n${e.code}\n${e.message}\n${e.details}";
});
} catch (e) {
print('caught generic exception');
print(e);
}
},
),
],
),
),
),
),
);
}
}
Get fruits button and see it doesn't update text view and instead throws an exception.I/flutter ( 9134): caught firebase functions exception
I/flutter ( 9134): unavailable
I/flutter ( 9134): UNAVAILABLE
pubspec.yaml, comment out firebase_core and change to cloud_functions: ^0.5.0 (earlier version) cloud_functions: ^0.5.0
# firebase_core: ^0.5.2
firebse_core)// Copyright 2018, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:cloud_functions/cloud_functions.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<dynamic> listOfFruits;
String exceptionMessage = '';
@override
Widget build(BuildContext context) {
final HttpsCallable callable = CloudFunctions.instance
.getHttpsCallable(functionName: 'listFruit')
..timeout = const Duration(seconds: 30);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Cloud Functions example app'),
),
body: Center(
child: Container(
margin: const EdgeInsets.only(top: 32.0, left: 16.0, right: 16.0),
child: Column(
children: [
Text(listOfFruits.toString() ?? exceptionMessage),
MaterialButton(
child: const Text('Get Fruits'),
onPressed: () async {
try {
final HttpsCallableResult result = await callable.call();
print(result.data);
listOfFruits = [];
setState(() {
for (var item in result.data) {
listOfFruits.add(item);
}
});
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
setState(() {
listOfFruits = null;
exceptionMessage =
"caught firebase functions exception\n${e.code}\n${e.message}\n${e.details}";
});
} catch (e) {
print('caught generic exception');
print(e);
}
},
),
],
),
),
),
),
);
}
}
Get fruits button and you will see text view is updated list of fruits
flutter doctor -v
[✓] Flutter (Channel stable, 1.22.3, on Microsoft Windows [Version 10.0.19042.610], locale en-US)
• Flutter version 1.22.3 at C:\Code\flutter_stable
• Framework revision 8874f21e79 (12 days ago), 2020-10-29 14:14:35 -0700
• Engine revision a1440ca392
• Dart version 2.10.3
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:\Code\sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Code\android-studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[!] Android Studio (version 4.1.0)
• Android Studio at C:\Code\android-studio
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[✓] VS Code (version 1.51.0)
• VS Code at C:\Users\taha\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.16.0
[✓] Connected device (1 available)
• RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29)
! Doctor found issues in 1 category.
Click To Expand
Dart SDK 2.10.3
Flutter SDK 1.22.3
triage 1.0.0+1
dependencies:
- cloud_functions 0.7.0+1 [meta flutter firebase_core firebase_core_platform_interface cloud_functions_platform_interface cloud_functions_web]
- cupertino_icons 0.1.3
- firebase_core 0.5.2 [firebase_core_platform_interface flutter quiver meta firebase_core_web]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]
dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data]
transitive dependencies:
- async 2.5.0-nullsafety.1 [collection]
- boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner]
- characters 1.1.0-nullsafety.3
- charcode 1.2.0-nullsafety.1
- clock 1.1.0-nullsafety.1
- cloud_functions_platform_interface 3.0.1 [flutter meta firebase_core plugin_platform_interface]
- cloud_functions_web 3.0.1 [firebase_core cloud_functions_platform_interface flutter flutter_web_plugins firebase http_parser meta]
- collection 1.15.0-nullsafety.3
- fake_async 1.2.0-nullsafety.1 [clock collection]
- firebase 7.3.0 [http http_parser js]
- firebase_core_platform_interface 2.0.0 [flutter meta plugin_platform_interface quiver]
- firebase_core_web 0.2.1 [firebase_core_platform_interface flutter flutter_web_plugins meta js]
- flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math]
- http 0.12.1 [http_parser path pedantic]
- http_parser 3.1.4 [charcode collection source_span string_scanner typed_data]
- js 0.6.1+1
- matcher 0.12.10-nullsafety.1 [stack_trace]
- meta 1.3.0-nullsafety.3
- path 1.8.0-nullsafety.1
- pedantic 1.9.0
- plugin_platform_interface 1.0.2 [meta]
- quiver 2.1.3 [matcher meta]
- sky_engine 0.0.99
- source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph]
- stack_trace 1.10.0-nullsafety.1 [path]
- stream_channel 2.1.0-nullsafety.1 [async]
- string_scanner 1.1.0-nullsafety.1 [charcode source_span]
- term_glyph 1.2.0-nullsafety.1
- test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner
term_glyph matcher]
- typed_data 1.3.0-nullsafety.3 [collection]
- vector_math 2.1.0-nullsafety.3```
I can reproduce this on the the latest flutter master 1.24.0-8.0.pre.194 with cloud_functions: ^0.7.0 but not with cloud_functions: ^0.6.0+1.
flutter doctor -v
[√] Flutter (Channel master, 1.24.0-8.0.pre.194, on Microsoft Windows [Version 10.0.19041.572], locale et-EE)
• Flutter version 1.24.0-8.0.pre.194 at C:\Development\flutter_master
• Framework revision 018467cdb1 (2 hours ago), 2020-11-11 02:04:03 -0500
• Engine revision 81f219c59c
• Dart version 2.12.0 (build 2.12.0-31.0.dev)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:\Users\marku\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.7.30621.155
• Windows 10 SDK version 10.0.18362.0
[√] Android Studio (version 4.1.0)
• Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] IntelliJ IDEA Ultimate Edition (version 2020.2)
• IntelliJ at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\202.7660.26
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
[√] Connected device (5 available)
• SM G950F (mobile) • ce12171c51cc001c03 • android-arm64 • Android 9 (API 28)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19041.572]
• Web Server (web) • web-server • web-javascript • Flutter Tools
• Chrome (web) • chrome • web-javascript • Google Chrome 86.0.4240.183
• Edge (web) • edge • web-javascript • Microsoft Edge 86.0.622.63
• No issues found!
I also got this when try to run flutter run
Note: /home/bolt/dev/data/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_functions-0.7.0+1/android/src/main/java/io/flutter/plugins/firebase/functions/FlutterFirebaseFunctionsPlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
pubspec.yml
firebase_core: 0.5.2
cloud_functions: 0.7.0+1
[✓] Flutter (Channel stable, 1.22.4, on Linux, locale en_US.UTF-8)
• Flutter version 1.22.4 at /home/bolt/dev/data/flutter
• Framework revision 1aafb3a8b9 (5 days ago), 2020-11-13 09:59:28 -0800
• Engine revision 2c956a31c0
• Dart version 2.10.4
Android Studio 4.1
Most helpful comment
I can reproduce this on the the latest flutter master
1.24.0-8.0.pre.194withcloud_functions: ^0.7.0but not withcloud_functions: ^0.6.0+1.flutter doctor -v