简体   繁体   中英

The operation 'uploadBytesResumable' cannot be performed on a root reference, create a non-root reference using child

I was using Firebase services without rules for testing like this: allow read, write: if true; and when I decide to set rules this problem happened suddenly!

allow read, write: if request.auth;= null;

FirebaseError: Firebase Storage: The operation 'uploadBytesResumable' cannot be performed on a root reference, create a non-root reference using child, such as.child('file.png'). (storage/invalid-root-operation)

Before changing the rules, I uploaded many images to storage and view them in my app, but now I'm trying with all my information without any changes!!

This is the method which I use to upload the images, I didn't change anything in it before or after changing the rules:

Reference reference = FirebaseStorage.instance.ref();

  Future uploadImage() async {
    reference.child('Items/${itemNameController.text}.jpg');
    await reference.putData(
      webImage!,
      SettableMetadata(contentType: 'image/jpeg'),
    );
    downloadUrl = await reference.getDownloadURL();
    update();
  }

By the way, I'm using Flutter web.

And I'm using this package: firebase_storage: ^11.0.6

Update

I will put here the full code for my operation, the problem is happening when I try to upload to fireStorage, but there is another function to store data in fireStore.

I have tried the solution that typed down by Someone, but still have the same problem when I combine functions (FireStorage - FireStore) with each other:

 Database database = Database();
  String? id;

  Future saveToDb() async {
    var formData = formKey.currentState;
    if (formData!.validate()) {
      await uploadImage().whenComplete(() async {
        await addToFireStore();
        clearData();
        Get.snackbar(
          'Adding done',
          'Adding item done successfully',
          snackPosition: SnackPosition.BOTTOM,
          backgroundColor: AppColors.mainColor,
          borderRadius: Dimensions.radius20,
          duration: const Duration(seconds: 3),
          padding: EdgeInsets.fromLTRB(
            Dimensions.width30,
            Dimensions.height20,
            Dimensions.width40,
            Dimensions.height20,
          ),
        );
        update();
      });
    }
  }

  Future uploadImage() async {
    await database.reference.child('Items/${itemNameController.text}.jpg').putData(
          webImage!,
          SettableMetadata(contentType: 'image/jpeg'),
        );
    downloadUrl = await database.reference.getDownloadURL();
    update();
  }

  Future addToFireStore() async {
    for (var i in database.categoriesModel) {
      if (database.categoryValue == i.name) {
        id = i.id;
      }
    }
    await database.catCollectionRef
        .doc(id)
        .collection('Items')
        .doc(const Uuid().v4())
        .set({
      'id': const Uuid().v4(),
      'Name': itemNameController.text,
      'Decryption': itemDescController.text,
      'Availability': switchValue.toString(),
      'Category': database.categoryValue,
      'Quantity type': quantityValue,
      'Quantity value': itemQuanController.text,
      'Price': itemPriceController.text,
      'Image': downloadUrl,
    });
    update();
  }

  clearData() {
    webImage?.clear();
    itemNameController.clear();
    itemDescController.clear();
    itemQuanController.clear();
    itemPriceController.clear();
    database.categoryValue = 'Choose one';
    switchValue = false;
    quantityValue = 'Choose One';
    downloadUrl = '';
    update();
  }

I use the saveToDb() function in the UI button to set the whole operation, but returns the same problem!!!

you're referencing to the root directlt, calling the child() on statement will not change the reference, try:

   Reference reference = FirebaseStorage.instance.ref();

  Future uploadImage() async {
    
    await reference.child('Items/${itemNameController.text}.jpg').putData(
      webImage!,
      SettableMetadata(contentType: 'image/jpeg'),
    );
    downloadUrl = await reference.getDownloadURL();
    update();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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