我正在尝试创建带有flutter的windows应用程序,我希望使用firebase进行身份验证,并使用防火墙来存储数据,那么如何将其集成起来呢?
发布于 2021-09-17 10:24:53
-首先,在依赖项上使用火镖插件。将http包文件导入文件并更新pubspec.yaml文件。
dependencies:
http:-第二,使用HTTP请求处理颤振桌面。为了避免命名冲突,我们增加了http。
import 'package:http/http.dart' as http;-生成生成post请求的请求,我们需要我们的数据库的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”用作字段,用于存储具有由防火墙自动生成的唯一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的空列表,它是一个小部件列表,每当用户添加新配置文件时都会更新它。最终列表loadedProfile = [];
我们将从数据库中获取JSON数据,因此,我们需要添加decode()语句。注意:要使用encode()和decode()语句,我们需要导入
import 'dart:convert';这里,extractedData是一个存储概要文件的变量。响应的每个元素都需要转换成一个小部件来为用户提供响应性的UI,因此我们使用了forEach()和add()语句来标识每个新的配置文件并将配置文件小部件添加到loadedProfile列表中最终的extractedData = json.decode(response.bo )
在这个小部件中准备UI NewUser widget时,我们有三个TextField()和一个FlatButton(),它们将接受用户的添加并在按下FlatButton()时生成get请求。现在我们需要三个不同的控制器
TextField().
final firstNameController = TextEditingController();
final lastNameController = TextEditingController();
final emailController = TextEditingController();https://stackoverflow.com/questions/69221292
复制相似问题