firebase_storage/object-not-found. No object exists at the desired reference

43 Views Asked by At

I am trying to fetch images from firebase storage and display it in my flutter app but i am getting the error "No object exists at the desired reference". All the images are stored in the root directory. I have also changed the firebase rules to allow read and write without authentication. This is my code.

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:firebase_storage/firebase_storage.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Wallpaper App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: WallpaperScreen(),
    );
  }
}

class WallpaperScreen extends StatefulWidget {
  @override
  _WallpaperScreenState createState() => _WallpaperScreenState();
}

class _WallpaperScreenState extends State<WallpaperScreen> {
  List<String> imageUrls = []; // Store fetched image URLs

  @override
  void initState() {
    super.initState();
    fetchImages();
  }

  Future<void> fetchImages() async {
    try {
      final storageRef = FirebaseStorage.instance.ref();
      final result = await storageRef.listAll();

      for (var item in result.items) {
        try {
          final url = await item.getDownloadURL();
          setState(() {
            imageUrls.add(url);
          });
        } catch (error) {
          print("Error fetching URL");
        }
      }
    } catch (e) {
      print("Error fetching images: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Wallpapers'),
      ),
      body: GridView.builder(
        itemCount: imageUrls.length,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 4.0,
          mainAxisSpacing: 4.0,
        ),
        itemBuilder: (BuildContext context, int index) {
          return Image.network(
            imageUrls[index], // Load image from URL
            fit: BoxFit.cover,
          );
        },
      ),
    );
  }
}
1

There are 1 best solutions below

1
meme page official On

The error "No object exists at the desired reference" in Firebase Storage usually indicates that the path you provided does not correspond to any object in the storage bucket. Here are a few things to check and troubleshoot:

  1. Check Firebase Storage Path: Ensure that the path you're providing to FirebaseStorage.instance.ref() matches the path where your images are stored in Firebase Storage. You mentioned that your images are stored in the root directory, so the path should indeed be correct.

  2. Verify Firebase Storage Rules: Even though you mentioned that you've set Firebase Storage rules to allow read and write without authentication, it's a good idea to double-check them to ensure they are correctly configured. Here's an example of rules that allow public read access to all files:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read;
      allow write: if false; // or any other conditions for write operations
    }
  }
}
  1. Check for Errors in URL Fetching: Although you have a catch block to handle errors when fetching URLs, you might want to print out the specific error messages to help with troubleshooting. Modify the catch block in fetchImages() like this:
        } catch (error) {
          print("Error fetching URL: $error");
        }

This way, you can see the exact error message if there are any issues with fetching the download URLs.

  1. Verify Firebase Initialization: Ensure that Firebase is initialized correctly in your app. You've already done this in your main() function, but it's good to double-check.

If you've verified all of the above and you're still encountering issues, try checking the Firebase console for any error logs or additional information that might help pinpoint the problem. Also, make sure that your Firebase project is properly set up and linked to your Flutter app.