繁体   English   中英

输入'未来<dynamic> ' 不是类型 'Future 的子类型<string> '</string></dynamic>

[英]type 'Future<dynamic>' is not a subtype of type 'Future<String>'

您好,我尝试将图像上传到 Firestore 的存储

我尝试使用此代码到 select 并上传带有多数据的图像以发送,但错误在于图像

我得到图像的代码

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发帖的代码

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 中调用代码

用这个表格

CustomButton(
                      onPressed: () {
                        controller.addDataToFireStore(
                          '${methodGetimage(controller)}',
                          name.text,
                          description.text,
                          price.text,
                          number.text,
                        );
                      },
                      text: 'add post',
                      alignment: Alignment.center,
                    ),

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

 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;
  }

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

methodGetimage是一个async function,您不要使用await来获取它的结果。

而不是这段代码:

onPressed: () {
  controller.addDataToFireStore(
    '${methodGetimage(controller)}',
    name.text,
    description.text,
    price.text,
    number.text,
  );
},

尝试这个:

onPressed: () {
  controller.addDataToFireStore(
    await methodGetimage(controller), // note the await keyword here
    name.text,
    description.text,
    price.text,
    number.text,
  );
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM