簡體   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