Flutter Firebase数据库颤振
我正在利用Flatter制作一个应用程序,在这个应用程序中,我需要从firebase数据库Flatter的每一次收集中获取数据。解决方案,如果你有一个Flutter Firebase数据库颤振,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我正在利用Flatter制作一个应用程序,在这个应用程序中,我需要从firebase数据库Flatter的每一次收集中获取数据。解决方案,如果你有一个 postref.document(post.authorId) .collection ('users'). documents ('Posts') .COLLECTION ('$DateTime').add({ 要从FIREBASE获取数据,请执行以下操作: 创建一个单独的文件,并将其命名为firebase_reference
postref.document(post.authorId)
.collection ('users'). documents ('Posts')
.COLLECTION ('$DateTime').add({
要从FIREBASE获取数据,请执行以下操作: 创建一个单独的文件,并将其命名为firebase_references.dart。在该文件中,将此引用写入数据库:
import 'package:cloud_firestore/cloud_firestore.dart';
final databaseReference = Firestore.instance;
现在,您可以在任何地方使用此引用来获取数据并对其进行操作/使用,如下所示:
import 'package:image/image.dart' as Img;
import 'package:path_provider/path_provider.dart';
compressImage() async {
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
Img.Image imageFile = Img.decodeImage(imgToUpload.readAsBytesSync());
final compressedImageFile = File('$path/img_name.jpg')
..writeAsBytesSync(Img.encodeJpg(imageFile, quality: 85));
setState(() {
imgToUpload = compressedImageFile;
});
}
假设您要在名为first_page.dart的文件中获取数据,请在该文件中键入以下代码:
将此函数置于任何小部件的构建函数之外:
别忘了在这里导入firebase_references.dart
getData() async {
DocumentSnapshot snapshot = await databaseReference
.collection("NAME_OF_COLLECTION")
.document("NAME_OF_DOCUMENT")
.get();
print(snapshot.data); //snapshot.data is what you want, use this however you want.
}
要更新FIREBASE中的数据,请执行以下操作:
正如你提到的,你想让用户上传多张照片到数据库,让用户直接上传文件到firestore不是一个好的做法,首先,firestore允许你上传文件到数据库,但有一些限制。例如,你不能将超过2MB的照片上传到它。
解决方案是将图像/文件上载到firebase存储,从那里获取url并将url发布到数据库。具体如下:
import 'package:image/image.dart' as Img;
import 'package:path_provider/path_provider.dart';
compressImage() async {
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
Img.Image imageFile = Img.decodeImage(imgToUpload.readAsBytesSync());
final compressedImageFile = File('$path/img_name.jpg')
..writeAsBytesSync(Img.encodeJpg(imageFile, quality: 85));
setState(() {
imgToUpload = compressedImageFile;
});
}
就像我写的关于制作一个单独的文件作为数据库参考的方式一样,我建议制作另一个文件作为存储参考。这应该是这样的:
import 'package:firebase_storage/firebase_storage.dart';
final StorageReference storageReference = FirebaseStorage.instance.ref();
现在是主要部分,让用户选择一张照片并对其进行压缩,以便我们可以将其发布到firebase存储,并获得一个URL,稍后我们可以将其发布到数据库
在first_page.dart中,写出这些函数,我将简要介绍它们的外观,以及ofc,它们应该在构建函数之外:
步骤1:定义一个变量,如File imgToUpload
步骤2:让用户拍照:
//This illustrates how to take a photo input using image_picker, you can do the same for the gallery too.
handleTakePhoto() async {
Navigator.pop(context);
File file = await ImagePicker.pickImage(
source: ImageSource.camera,
maxHeight: 500,
maxWidth: 500,
);
setState(() {
this.imgToUpload = file;
});
}
第三步:压缩图像。
为此,您需要按如下方式导入:
import 'package:image/image.dart' as Img;
import 'package:path_provider/path_provider.dart';
compressImage() async {
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
Img.Image imageFile = Img.decodeImage(imgToUpload.readAsBytesSync());
final compressedImageFile = File('$path/img_name.jpg')
..writeAsBytesSync(Img.encodeJpg(imageFile, quality: 85));
setState(() {
imgToUpload = compressedImageFile;
});
}
步骤4:上载图像并获取url:
Future<String> uploadImage(imageFile) async {
StorageUploadTask uploadTask =
storageReference.child("post_name.jpg").putFile(imageFile);
StorageTaskSnapshot storageSnap = await uploadTask.onComplete;
String downloadUrl = await storageSnap.ref.getDownloadURL();
return downloadUrl; //this is what you need.
}
您也可以根据自己的需要使用更新数据。请提供有关您在哪里遇到问题的更多详细信息?我需要每个集合的数据,动态集合的数据都是大写的。好的,我将尝试键入一个答案,让我知道它是否有帮助…Kool带来它如果你问如何列出嵌套在颤振应用程序文档下的子集合,那实际上是不可能的。您只能使用后端SDK来实现这一点。我正试图允许用户上传多张照片,任何解决方案,我必须能够压缩每一张照片,并保持在一种方式,它给我的用户上传回来