Firebase 如何在fire base中存储图像并在firestore中存储url

Firebase 如何在fire base中存储图像并在firestore中存储url,firebase,flutter,dart,Firebase,Flutter,Dart,我想将优惠券卡发送到fire商店,其中包含(姓名-地址-优惠券),我想让用户为每一张卡设置一个特定的图像 那是我的FireStoreService文件 class FireStoreService { FireStoreService._internal(); static final FireStoreService firestoreService = FireStoreService._internal(); Firestore db = Firestore.instance

我想将优惠券卡发送到fire商店,其中包含(姓名-地址-优惠券),我想让用户为每一张卡设置一个特定的图像

那是我的FireStoreService文件

class FireStoreService {

  FireStoreService._internal();
  static final FireStoreService firestoreService = FireStoreService._internal();
  Firestore db = Firestore.instance ;

  factory FireStoreService() {
    return firestoreService;
  }

  Stream<List<Coupon>> getCoupon() {
    return db.collection('coupon').snapshots().map(
          (snapshot) => snapshot.documents.map(
            (doc) => Coupon.fromMap(doc.data, doc.documentID),
      ).toList(),
    );
  }

  Future<void> addCoupon(Coupon coupon) {
    return db.collection('coupon').add(coupon.toMap());
  }

  Future<void> deleteCoupon(String id) {
    return db.collection('coupon').document(id).delete();
  }

  Future<void> updateCoupon(Coupon coupon) {
    return db.collection('coupon').document(coupon.id).updateData(coupon.toMap());
  }



}

有什么帮助吗?

这是一个请求图像文件的函数。如果运行代码(getImage函数):将image变量传递给uploadImage函数

String myUrl = await uploadImage(file);
然后可以使用setData或updateData将url放入数据库中

  Firestore.instance.collection('books').document()
      .setData({ 'title': 'title', 'url': '$myUrl' })


final StorageReference storageRef = FirebaseStorage.instance.ref();

  Future<String> uploadImage(imageFile) async {
    StorageUploadTask uploadTask =
        storageRef.child("myPath&Name.jpg").putFile(imageFile);
    StorageTaskSnapshot storageSnap = await uploadTask.onComplete;
    String downloadURL = await storageSnap.ref.getDownloadURL();
    return downloadURL;
  }
Firestore.instance.collection('books').document()
.setData({'title':'title','url':'$myUrl'})
final-StorageReference-storageRef=FirebaseStorage.instance.ref();
未来上载映像(图像文件)异步{
StorageUploadTask上传任务=
storageRef.child(“myPath&Name.jpg”).putFile(imageFile);
StorageTaskSnapshot storageSnap=等待上载Task.onComplete;
String downloadURL=await-storageSnap.ref.getDownloadURL();
返回下载URL;
}
String myUrl = await uploadImage(file);
  Firestore.instance.collection('books').document()
      .setData({ 'title': 'title', 'url': '$myUrl' })


final StorageReference storageRef = FirebaseStorage.instance.ref();

  Future<String> uploadImage(imageFile) async {
    StorageUploadTask uploadTask =
        storageRef.child("myPath&Name.jpg").putFile(imageFile);
    StorageTaskSnapshot storageSnap = await uploadTask.onComplete;
    String downloadURL = await storageSnap.ref.getDownloadURL();
    return downloadURL;
  }