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,
);
},
),
);
}
}
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:
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.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:
fetchImages()like this:This way, you can see the exact error message if there are any issues with fetching the download URLs.
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.