【发布时间】:2021-09-17 09:49:31
【问题描述】:
我正在尝试使用颤振创建 Windows 应用程序,我想使用 firebase 进行身份验证和使用 firestore 存储数据,那么我该如何整合它呢?
【问题讨论】:
标签: firebase flutter desktop-application flutter-desktop
我正在尝试使用颤振创建 Windows 应用程序,我想使用 firebase 进行身份验证和使用 firestore 存储数据,那么我该如何整合它呢?
【问题讨论】:
标签: firebase flutter desktop-application flutter-desktop
-- 首先,在依赖项上使用firedart 插件。 将 http 包文件导入您的文件并更新您的 pubspec.yaml 文件。
dependencies:
http:
-- 其次,flutter桌面使用HTTP请求。我们已添加为 http 以避免命名冲突。
import 'package:http/http.dart' as http;
-- 生成请求 要生成发布请求,我们需要数据库的 URL,该 URL 位于数据库屏幕的顶部。
http.post("https://fir-flutted60b0.firebaseio.com/userprofile.json",
body: json.encode());
or
url =" https://fir-flutted60b0.firebaseio.com/userprofile.json";
http.post(url,body:json.encode());
这里我们还在 URL 的末尾添加了“/userprofile.json”。添加这一点非常重要,因为名称“userprofile”用作存储各种属性的字段,该字段具有由 firebase 自动生成的唯一 ID。
encode() 语句用于将数据转换为 JSON 格式。
-- 代码实现:
sendData() {
http.post("https://fir-flutter-d60b0.firebaseio.com/userprofile.json",
body: json.encode({
'firstName': firstNameController.text,
'lastName': lastNameController.text,
'email': emailController.text,
}));
setState(() {
userProfile.add(Profile(
firstName: firstNameController.text,
lastName: lastNameController.text,
email: emailController.text,
));
});
}
-- 发送 GET 请求 要获取数据,我们将再次需要相同的 URL。
最终响应 = 等待 http.get("https://fir-flutter d60b0.firebaseio.com/userprofile.json?"); 在这里,我们采用了一个名为loadedProfile 的空列表,它是一个小部件列表,每次用户添加新配置文件时都会更新。 最终列表加载配置文件 = [];
我们将从数据库中获取 JSON 数据,因此,我们需要添加一个 decode() 语句。 注意:要使用 encode() 和 decode() 语句,我们需要导入
import 'dart:convert';
这里extractData 是一个将存储配置文件的变量。响应的每个元素都需要转换为小部件,以便为用户提供响应式 UI,因此我们使用了 forEach() 和 add() 语句来识别每个新配置文件并将配置文件小部件添加到加载的配置文件列表中 最终提取数据 = json.decode(response.bo
准备界面 新用户小部件 在这个小部件中,我们有三个 TextField() 和一个 FlatButton(),它们将从用户那里获取 add 并在按下 FlatButton() 时生成一个 get 请求。 现在我们需要三个不同的控制器来处理这三个
TextField().
final firstNameController = TextEditingController();
final lastNameController = TextEditingController();
final emailController = TextEditingController();
【讨论】: