首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'Future<dynamic>‘不是'Future<String>’的子类型

类型'Future<dynamic>‘不是'Future<String>’的子类型
EN

Stack Overflow用户
提问于 2022-09-01 09:16:20
回答 1查看 64关注 0票数 0

你好,我试着把图片上传到消防局的存储

Am尝试使用此代码选择和上载多个数据要发送的图像,但错误与图像有关。

我得到图像的代码

代码语言:javascript
复制
void getImage(ImageSource imageSource) async {
    final pickedFile = await ImagePicker().pickImage(source: imageSource);

    if (pickedFile != null) {
      selectedImagePath.value = pickedFile.path;
    } else {
      Get.snackbar("Error", "No Image selected",
          snackPosition: SnackPosition.BOTTOM,
          backgroundColor: Colors.red,
          colorText: Colors.white);
    }
  }

这也是我发帖子到fireStore的代码

代码语言:javascript
复制
final CollectionReference _userCollectionRef =
      FirebaseFirestore.instance.collection('Cars');
  ProductModel? productModel;
  Future<DocumentReference<Object?>> addDataToFireStore(
    String image,
    String name,
    String description,
    String price,
    String number,
  ) async {
    return await _userCollectionRef.add({
      'name': name,
      'description': description,
      'image': image,
      'price': price,
      'country': box.read(NAME_OF_CONTRY),
      'number': number,
      'uid': box.read('uid'),
    });
  }

我在代码的UI中调用代码

用这个表格

代码语言:javascript
复制
CustomButton(
                      onPressed: () {
                        controller.addDataToFireStore(
                          '${methodGetimage(controller)}',
                          name.text,
                          description.text,
                          price.text,
                          number.text,
                        );
                      },
                      text: 'add post',
                      alignment: Alignment.center,
                    ),

我已经创建了一个方法,当图像上传到存储区时返回它的URL

代码语言:javascript
复制
 methodGetimage(ImageViewModel controller) async {
    var randome = Random().nextInt(1000000);

    File file = File(controller.selectedImagePath.value);
    var imagename = basename(controller.selectedImagePath.value);

    imagename = '$imagename$randome';
    var refstoreg = FirebaseStorage.instance.ref('cars/$imagename');
    await refstoreg.putFile(file);

    var url = await refstoreg.getDownloadURL();

    return url;
  }

当我上传时,在消防局的图像中有一个文本,它是Instance of 'Future<String>'

EN

回答 1

Stack Overflow用户

发布于 2022-09-01 12:37:03

methodGetimage是一个async函数,您不使用await来获得它的结果。

而不是这段代码:

代码语言:javascript
复制
onPressed: () {
  controller.addDataToFireStore(
    '${methodGetimage(controller)}',
    name.text,
    description.text,
    price.text,
    number.text,
  );
},

试试这个:

代码语言:javascript
复制
onPressed: () {
  controller.addDataToFireStore(
    await methodGetimage(controller), // note the await keyword here
    name.text,
    description.text,
    price.text,
    number.text,
  );
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73566946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档