Flutter
Parse SDK (REST)
在Flutter应用中实现Parse云代码函数指南
17 分
在 flutter 应用中使用云函数 介绍 对于复杂的应用,有时你只需要一些不在移动设备上运行的逻辑。云代码使这一切成为可能。 云代码易于使用,因为它建立在支持数千个应用的同一 parse javascript sdk 上。唯一的区别是,这段代码在你的 parse 服务器上运行,而不是在用户的移动设备上运行。 你可以使用云代码将处理卸载到 parse 服务器,从而提高应用的感知性能。你可以创建在对象被保存或删除时运行的钩子。如果你想验证或清理数据,这非常有用。你还可以使用云代码修改相关对象或启动其他过程,例如发送推送通知。 当你更新云代码时,它会立即对所有移动环境可用。你不必等待应用程序的新版本发布。这让你可以即时更改应用行为并更快地添加新功能。 本节解释如何创建和部署云代码,然后介绍如何通过 back4app 在 flutter 项目中调用云函数。 在本指南中,重点是通过 flutter 演示云函数的使用。你可以在 https //docs parseplatform org/cloudcode/guide/ 中找到更深入的信息。 前提条件 要完成本教程,您需要: https //flutter dev/docs/get started/install https //developer android com/studio 或 https //code visualstudio com/ (带有 https //docs flutter dev/get started/editor dart 和 flutter) 一个在 back4app 上 https //www back4app com/docs/get started/new parse app : 注意: 请遵循 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 上创建 parse 应用。 一个连接到 back4app 的 flutter 应用。 注意: 请遵循 https //www back4app com/docs/flutter/parse sdk/parse flutter sdk 以创建一个连接到 back4app 的 flutter 项目。 一台运行 android 或 ios 的设备(或虚拟设备)。 目标 从 flutter 应用程序在 back4app 上运行 parse cloud 代码。 1 创建一个 cloud code 文件 访问您的应用程序,网址为 https //www back4app com/ 并点击 服务器设置 服务器设置 。 找到 cloud code cloud code 并点击 函数和网络托管 函数和网络托管 。它看起来像这样 3\ 上传或创建一个新文件(您也可以直接在浏览器中编辑当前 main js main js 文件)。然后,点击 部署 部署 如下所示 您的 main js main js 文件应该看起来像这样 1 parse cloud define("hello", async (request) => { 2 console log("hello from cloud code!"); 3 return "hello from cloud code!"; 4 }); 5 6 parse cloud define("sumnumbers", async (request) => { 7 return (request params number1 + request params number2); 8 }); 9 10 parse cloud define("createtodo", async (request) => { 11 const title = request params title; 12 const done = request params done; 13 14 const todo = parse object extend('todo'); 15 const todo = new todo(); 16 todo set('title', title); 17 todo set('done', done); 18 19 try { 20 await todo save(); 21 return todo; 22 } catch (error) { 23 console log('todo create error ' + error code + ' ' + error message); 24 } 25 }); 26 27 parse cloud define("getlisttodo", async (request) => { 28 let query = new parse query("todo"); 29 query descending("done"); 30 return await query find(); 31 }); 您可以从 flutter 应用程序向云函数传递参数,并在 request params request params 对象中访问它们。 2 理解 parsecloudfunction 类 该 parsecloudfunction parsecloudfunction 类定义了与 parse cloud functions 交互的方法。 可以使用 parsecloudfunction execute({parameters params}) parsecloudfunction execute({parameters params}) 调用云函数,该函数返回一个映射对象,或者 parsecloudfunction executeobjectfunction<>({parameters params}) parsecloudfunction executeobjectfunction<>({parameters params}) 返回一个 parseobject。 参数是可选的,期望返回一个映射对象。 3 调用 parse cloud 函数 现在您已经部署了云函数,我们可以使用 flutter 调用这些函数。 示例 1 调用云函数并获取返回值 1 //executes a cloud function with no parameters that returns a map object 2 final parsecloudfunction function = parsecloudfunction('hello'); 3 final parseresponse parseresponse = await function execute(); 4 if (parseresponse success && parseresponse result != null) { 5 print(parseresponse result); 6 } 控制台显示的结果将是: 1 示例 2 调用带参数的云函数并获取返回值 1 //executes a cloud function with parameters that returns a map object 2 final parsecloudfunction function = parsecloudfunction('sumnumbers'); 3 final map\<string, dynamic> params = \<string, dynamic>{ 4 'number1' 10, 5 'number2' 20 6 }; 7 final parseresponse parseresponse = 8 await function execute(parameters params); 9 if (parseresponse success) { 10 print(parseresponse result); 11 } 控制台显示的结果将是: flutter 来自云代码的问候 ! 示例 2 1 调用带参数的云函数并获取返回值 1 //executes a cloud function with parameters that returns a map object 2 final parsecloudfunction function = parsecloudfunction('sumnumbers'); 3 final map\<string, dynamic> params = \<string, dynamic>{ 4 'number1' 10, 5 'number2' 20 6 }; 7 final parseresponse parseresponse = 8 await function execute(parameters params); 9 if (parseresponse success) { 10 print(parseresponse result); 11 } flutter 30 示例 3 调用带参数的云函数并获取 parseobject 作为返回 1 //executes a cloud function that returns a parseobject type 2 final parsecloudfunction function = parsecloudfunction('createtodo'); 3 final map\<string, dynamic> params = \<string, dynamic>{ 4 'title' 'task 1', 5 'done' false 6 }; 7 final parseresponse parseresponse = 8 await function executeobjectfunction\<parseobject>(parameters params); 9 if (parseresponse success && parseresponse result != null) { 10 if (parseresponse result\['result'] is parseobject) { 11 //transforms the return into a parseobject 12 final parseobject parseobject = parseresponse result\['result']; 13 print(parseobject objectid); 14 } 15 } 控制台中显示的结果将是 flutter {"classname" "todo","objectid" "h0khsir6kt","createdat" "2021 06 25t00 21 10 023z","updatedat" "2021 06 25t00 21 10 023z","title" "任务 1","done" \ false } 示例 4 调用返回可以转换为 parseobject 的地图列表的云函数 1 //executes a cloud function with parameters that returns a map object 2 final parsecloudfunction function = parsecloudfunction('getlisttodo'); 3 final parseresponse parseresponse = await function execute(); 4 if (parseresponse success) { 5 if (parseresponse result != null) { 6 for (final todo in parseresponse result) { 7 //use fromjson method to convert map in parseobject 8 print(parseobject('todo') fromjson(todo)); 9 } 10 } 11 } 控制台中显示的结果将是 1 flutter {"classname" "todo","objectid" "cr3g4rcct1","createdat" "2021 06 23t03 20 34 933z","updatedat" "2021 06 23t03 20 34 933z","title" "task 1","done"\ false} 2 flutter {"classname" "todo","objectid" "6barcicpke","createdat" "2021 06 23t03 20 54 294z","updatedat" "2021 06 23t03 20 54 294z","title" "task 1","done"\ false} 3 flutter {"classname" "todo","objectid" "tyza74l89q","createdat" "2021 06 23t03 39 42 049z","updatedat" "2021 06 23t03 39 42 049z","title" "task 1","done"\ false} 4 flutter {"classname" "todo","objectid" "arjm8q7h8d","createdat" "2021 06 24t03 33 27 925z","updatedat" "2021 06 24t03 33 27 925z","title" "task 1","done"\ false} 5 从 flutter 调用云函数 现在让我们在flutter应用中使用我们的示例调用云函数,配合一个简单的界面。 打开你的flutter项目,去到 main dart main dart 文件,清理所有代码,并用以下内容替换它: 1 import 'package\ flutter/cupertino dart'; 2 import 'package\ flutter/material dart'; 3 import 'package\ parse server sdk flutter/parse server sdk dart'; 4 5 void main() async { 6 widgetsflutterbinding ensureinitialized(); 7 8 final keyapplicationid = 'your app id here'; 9 final keyclientkey = 'your client key here'; 10 11 final keyparseserverurl = 'https //parseapi back4app com'; 12 13 await parse() initialize(keyapplicationid, keyparseserverurl, 14 clientkey keyclientkey, debug true); 15 16 runapp(materialapp( 17 title 'flutter geopoint', 18 debugshowcheckedmodebanner false, 19 home homepage(), 20 )); 21 } 22 23 class homepage extends statefulwidget { 24 @override 25 homepagestate createstate() => homepagestate(); 26 } 27 28 class homepagestate extends state\<homepage> { 29 30 void docallcloudcodehello() async { 31 //executes a cloud function with no parameters that returns a map object 32 final parsecloudfunction function = parsecloudfunction('hello'); 33 final parseresponse parseresponse = await function execute(); 34 if (parseresponse success && parseresponse result != null) { 35 print(parseresponse result); 36 } 37 } 38 39 void docallcloudcodesumnumbers() async { 40 //executes a cloud function with parameters that returns a map object 41 final parsecloudfunction function = parsecloudfunction('sumnumbers'); 42 final map\<string, dynamic> params = \<string, dynamic>{ 43 'number1' 10, 44 'number2' 20 45 }; 46 final parseresponse parseresponse = 47 await function execute(parameters params); 48 if (parseresponse success) { 49 print(parseresponse result); 50 } 51 } 52 53 void docallcloudcodecreatetodo() async { 54 //executes a cloud function that returns a parseobject type 55 final parsecloudfunction function = parsecloudfunction('createtodo'); 56 final map\<string, dynamic> params = \<string, dynamic>{ 57 'title' 'task 1', 58 'done' false 59 }; 60 final parseresponse parseresponse = 61 await function executeobjectfunction\<parseobject>(parameters params); 62 if (parseresponse success && parseresponse result != null) { 63 if (parseresponse result\['result'] is parseobject) { 64 //transforms the return into a parseobject 65 final parseobject parseobject = parseresponse result\['result']; 66 print(parseobject tostring()); 67 } 68 } 69 } 70 71 void docallcloudcodegetlisttodo() async { 72 //executes a cloud function with parameters that returns a map object 73 final parsecloudfunction function = parsecloudfunction('getlisttodo'); 74 final parseresponse parseresponse = await function execute(); 75 if (parseresponse success) { 76 if (parseresponse result != null) { 77 for (final todo in parseresponse result) { 78 //use fromjson method to convert map in parseobject 79 print(parseobject('todo') fromjson(todo)); 80 } 81 } 82 } 83 } 84 85 @override 86 widget build(buildcontext context) { 87 return scaffold( 88 body padding( 89 padding const edgeinsets all(16 0), 90 child column( 91 crossaxisalignment crossaxisalignment stretch, 92 children \[ 93 container( 94 height 200, 95 child image network( 96 'https //blog back4app com/wp content/uploads/2017/11/logo b4a 1 768x175 1 png'), 97 ), 98 center( 99 child const text('flutter on back4app call clode code', 100 style textstyle(fontsize 18, fontweight fontweight bold)), 101 ), 102 sizedbox( 103 height 8, 104 ), 105 container( 106 height 50, 107 child elevatedbutton( 108 onpressed docallcloudcodehello, 109 child text('cloud code hello'), 110 style elevatedbutton stylefrom(primary colors blue)), 111 ), 112 sizedbox( 113 height 8, 114 ), 115 container( 116 height 50, 117 child elevatedbutton( 118 onpressed docallcloudcodesumnumbers, 119 child text('cloud code sumnumber'), 120 style elevatedbutton stylefrom(primary colors blue)), 121 ), 122 sizedbox( 123 height 8, 124 ), 125 container( 126 height 50, 127 child elevatedbutton( 128 onpressed docallcloudcodecreatetodo, 129 child text('cloud code createtodo'), 130 style elevatedbutton stylefrom(primary colors blue)), 131 ), 132 sizedbox( 133 height 8, 134 ), 135 container( 136 height 50, 137 child elevatedbutton( 138 onpressed docallcloudcodegetlisttodo, 139 child text('cloud code getlisttodo'), 140 style elevatedbutton stylefrom(primary colors blue)), 141 ), 142 ], 143 ), 144 )); 145 } 146 } 找到您的应用程序 id 和客户端密钥凭据,导航到您的应用程序仪表板,网址为 https //www back4app com/ 在 main dart main dart 中更新您的代码,使用您项目的 applicationid 和 clientkey 的值,来自 back4app keyapplicationid = 应用程序 id 应用程序 id keyclientkey = 客户端密钥 客户端密钥 运行项目,应用程序将如图所示加载 结论 在这个阶段,您能够在 flutter 应用程序中使用 back4app 通过 parse server core 功能编写和调用自己的云代码!