StorageTaskSnapshot 在颤动中不起作用

时间:2021-05-02 06:43:16

标签: firebase flutter dart google-cloud-firestore

我正在尝试将我的图像存储在 firebase 中并检索它。但是我被卡住了,因为 StorgeTaskSnapshot 不起作用,它说“未定义的类/方法”。我如何解决它?是否是由于更新的软件包版本导致的另一个问题?

这是一张图片 - Please help me to resolve this

编辑:我忘记添加代码,所以我正在做 -

这是完整的文件 -

/R

请检查行 - 106-120、129-142 和 144-173、175-198,因为在这些行中,您可能会找到出现问题的代码,而您不必搜索它

这是我的日志 -

my_colors <- c(
  `liberal`        = "darkred",
  `conservative`   = "darkblue",
  `ndp`            = "orange",
  `bq`             = "cyan",
  `green`          = "darkgreen",
  `laurier1`       = "darkorchid",
  `laurier2`       = "gold")

#' My colors
#'
#' available colors vector
#'
#' @format a vector with the colors I need
#' @export

"my_colors"

1 个答案:

答案 0 :(得分:1)

如果您更新了依赖项,则可能是您使用了旧语法。

您能否尝试使用更新的上传语法。下面是一个例子:

import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

firebase_storage.FirebaseStorage storage =
    firebase_storage.FirebaseStorage.instance;

class StorageService {
  static Future<String> uploadFile(String path, File file) async {
    try {
      firebase_storage.Reference storageReference =
          firebase_storage.FirebaseStorage.instance.ref(path);
      await storageReference.putFile(file);

      String downloadURL = await storageReference.getDownloadURL();
      return downloadURL;
    } catch (e) {
      return null;
    }
  }
}

你像这样使用它:

String photoURL = await StorageService.uploadFile(
        'users/${_auth.currentUser.uid}/${Path.basename(_image.path)}', _image);

您的函数如下所示:

uploadImageToFirestoreAndStorage() async {
    String mFileName = id;
    Reference storageReference = FirebaseStorage.instance.ref('path_in_storage').child(mFileName);
    await storageReference.putFile(imageFileAvatar);

    String photoUrl = await storageReference.getDownloadURL();

    try{
    await FirebaseFirestore.instance.collection("users").doc(id).update({
      "photoUrl": photoUrl,
      "aboutMe": aboutMe,
      "nickname": nickname,
    });
    }catch(e){
    print('e $e');
    }

    print('photoUrl $photoUrl');
    await preferences.setString("photoUrl", photoUrl);

    setState(() {
      isLoading = false;
    });

    Fluttertoast.showToast(msg: "Updated successfully");
  }

当我开始从画廊更新我的个人资料图片时,这些是我的日志 -

I/fluttertelegra( 9924): NativeAlloc concurrent copying GC freed 6283(330KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3993KB/7987KB, paused 1.090ms total 114.133ms
D/UploadTask( 9924): Increasing chunk size to 524288
D/UploadTask( 9924): Increasing chunk size to 1048576
D/UploadTask( 9924): Increasing chunk size to 2097152
I/fluttertelegra( 9924): Background young concurrent copying GC freed 4870(2176KB) AllocSpace objects, 6(1624KB) LOS objects, 0% free, 8227KB/8227KB, paused 3.532ms total 119.502ms
I/fluttertelegra( 9924): Background concurrent copying GC freed 1139(152KB) AllocSpace objects, 1(260KB) LOS objects, 49% free, 7846KB/15MB, paused 11.888ms total 118.770ms
D/UploadTask( 9924): Increasing chunk size to 4194304
E/Toast   ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
E/Toast   ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
W/fluttertelegra( 9924): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed) 
E/Toast   ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used