Gigea Nicusor
Gigea Nicusor

Reputation: 15

Don't know why i can't show images from firebase

I try for some time to fetch some images from my firebase storage, but didn't get nowhere.

This is the code:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('I try so hard and got no far:<'),
        ),
        body: ImageList(),
      ),
    );
  }
}

class ImageList extends StatefulWidget {
  @override
  _ImageListState createState() => _ImageListState();
}

class _ImageListState extends State<ImageList> {
  List<String> imageUrls = [];

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

  Future<void> loadImages() async {
    // Replace 'YOUR_DIRECTORY_PATH_HERE' with the actual path in Firebase Storage.
    String directoryPath = 'Images/';

    Reference reference = FirebaseStorage.instance.ref().child(directoryPath);

    // List all items in the directory
    ListResult result = await reference.listAll();

    // Get the download URLs for each item
    List<String> urls = [];
    for (Reference ref in result.items) {
      String url = await ref.getDownloadURL();
      urls.add(url);
    }

    setState(() {
      imageUrls = urls;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: imageUrls.length,
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Image.network(
            imageUrls[index],
            width: 200.0,
            height: 200.0,
            fit: BoxFit.cover,
          ),
        );
      },
    );
  }
}

and the error i get is

The following ProgressEvent$ object was thrown resolving an image codec:
[object ProgressEvent]

When the exception was thrown, this was the stack:
Image provider: NetworkImage("https://firebasestorage.googleapis.com/v0/b/carousel-a996b.appspot.com/o/Images%2F1.png?alt=media&token=017d99a3-b909-4c2f-b2fe-e7ae1f282327", scale: 1.0)
Image key: NetworkImage("https://firebasestorage.googleapis.com/v0/b/carousel-a996b.appspot.com/o/Images%2F1.png?alt=media&token=017d99a3-b909-4c2f-b2fe-e7ae1f282327", scale: 1.0)

If i click the link, it will redirect me to the image in my firebase.

Upvotes: 0

Views: 361

Answers (1)

Gigea Nicusor
Gigea Nicusor

Reputation: 15

To resolve this problem in flutter in console i ran this command flutter run -d chrome --web-renderer html , and it resolved my problem for now, but for production will need to modify CORS configuration. Link for the post that helped me: EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE [object ProgressEvent]

Upvotes: 0

Related Questions