dart 我的FirebaseStorage方法未在Flutter中运行,在`firebase_storage之后:^4.0.0

iyfjxgzm  于 2023-07-31  发布在  Flutter
关注(0)|答案(2)|浏览(99)

我的问题

有人可以帮助我试图将我的图像发送到firebase存储,然后检索下载URL并保存到firebasefirestore。但是我的FirebaseStorage方法没有运行。
firebase_storage: ^4.0.0更新后发生
我的FirebaseStorage().ref().child('path).putFile()方法没有运行

FirebaseStorage()
             .ref()
             .child("Accounts Posts/Images/").putFile(//MyFIle);

字符串

这是我的代码

static final FirebaseStorage storage = FirebaseStorage.instance;
static final FirebaseFirestore _firestore = FirebaseFirestore.instance;
Future<dynamic> sendData(SearchTileModel st, List<File> data) async {
 bool completed = false;
 CollectionReference _collec = _firestore.collection('Accounts Posts');
 List<String> _imageUrls = [];
 print(data.length);
 data.length > 0
     ? data.asMap().forEach((
         index,
         element,
       ) async {
         print(index);
         print(element);
         String downloadUrl;
///////  HERE this code doesn't get called up  ////////////
         StorageTaskSnapshot snapshot = await FirebaseStorage()
             .ref()
             .child("Accounts Posts/Images/${index}")
             .putFile(element)
             .onComplete
             .then((value) {
           downloadUrl = value.ref.getDownloadURL().toString();
           return;
         });
         print(downloadUrl);
         print(snapshot);
         print(snapshot.error);
         if (snapshot.error == null) {
           await snapshot.ref
               .getDownloadURL()
               .then((value) => downloadUrl = value.toString());
           print(downloadUrl);
           _imageUrls.insert(index, downloadUrl);
           if (st.images.length == _imageUrls.length) {
             SearchTileModel newModel = st;
             newModel.images = _imageUrls;
             await _collec
                 .doc('${newModel.placeName} : name')
                .set(
                  st.toJson(),
                  SetOptions(mergeFields: [
                    'images',
                  ]),
                )
                .whenComplete(() {
              return completed = true;
            }).catchError((onError) {
              print('${onError.toString()}');
              completed = false;
            });
          }
        } else {
          completed = false;
          print('Error from image repo ${snapshot.error.toString()}');
          throw ('This file is not an image');
        }
      })
    : print('No Images Selected');
if (data.length == 0) {
  SearchTileModel newModel = st;
  newModel.images = _imageUrls;
  await _collec
      .doc('${newModel.placeName} : name')
      .set(
        st.toJson(),
        SetOptions(mergeFields: [
          'images',
        ]),
      )
      .whenComplete(() {
    return completed = true;
  }).catchError((onError) {
    print('${onError.toString()}');
    completed = false;
  });
}
return completed;
}

调试控制台输出

显示该方法未运行的所有print语句。我还认为,由于await,这是需要时间,但没有后,等待10分钟,什么也没有发生。

I/flutter (14005): true
  I/flutter (14005): redtfgn
  I/flutter (14005): File: 
  '/storage/emulated/0/Android/data/com.example.medium/files/Pictures/scaled_ec614e8a-0522-4cae-97be- 
  3ed50356de9c3343447263121135351.jpg'
  I/flutter (14005): 1
  I/flutter (14005): running
  I/flutter (14005): 0
  I/flutter (14005): File: 
  '/storage/emulated/0/Android/data/com.example.medium/files/Pictures/scaled_ec614e8a-0522-4cae-97be- 
  3ed50356de9c3343447263121135351.jpg'
  I/flutter (14005): Till Here the code is Running


更新得到这些行

I/BiChannelGoogleApi(17801): [FirebaseAuth: ] getGoogleApiForMethod() 
returned Gms: com.google.firebase.auth.api.internal.zzaq@cdf79f2
E/StorageUtil(17801): error getting token 
java.util.concurrent.TimeoutException: Timed out waiting for Task
D/NetworkSecurityConfig(17801): No Network Security Config specified, using 
platform default
W/NetworkRequest(17801): no auth token for request

Firebase存储规则也改了

rules_version = '2';
service firebase.storage {
 match /b/{bucket}/o {
   match /{allPaths=**} {
     allow read, write ;
   }
 }
}

ie3xauqp

ie3xauqp1#

我可以看到你正在使用**.ref()来获取引用。查看文档,可以看到使用的是getReference()**

StorageReference storageRef = storage.getReference();

字符串
因此,最好按照文档中的指示设置存储引用。上传文件的代码也是这样:

Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
uploadTask = riversRef.putFile(file);

j2qf4p5b

j2qf4p5b2#

而不是这个

FirebaseStorage()
             .ref()
             .child("Accounts Posts/Images/").putFile(//MyFIle);

字符串
试着把这个

FirebaseStorage()
             .ref()
             .child("Accounts Posts/Images/").putData(//MyFIle);

相关问题