admin管理员组

文章数量:1403522

I'm building a Flutter application where I store event data in Firebase, including an image URL for each event. The image paths are stored as strings in Firestore under the eventImage field (e.g., "assets/images/event.png"). However, when I attempt to display the image using the AssetImage widget, I am getting the following error: Unable to load asset: ""

   StreamBuilder<QuerySnapshot<Eventdata>>(
    stream: FirebaseFunctions.getstreamdata(),
      builder: (context, snapshot) {
      if (snapshot.hasError) {
      return Column(
        children: [
      Text("Something went wrong"),
      SizedBox(height: 12),
      IconButton(
        onPressed: () {},
        icon: Icon(
          Icons.refresh,
          color: Colors.blue,
        ),
      ),
    ],
      );
    }
    if (snapshot.connectionState == ConnectionState.waiting) {
     return const Center(
     child: CircularProgressIndicator(
      color: colorpallete.parimary,
     ),
     );
    } else {
     List<Eventdata> savedEvents = snapshot.data!.docs.map(
    (element) {
      var event = element.data();
      print("Fetched Event: ${event.eventimage}");
      return event;
    },
     ).toList();

    return savedEvents.isNotEmpty
      ? Expanded(
          child: ListView.separated(
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var event = savedEvents[index];
              return event_card(
                eventddatamodel: event,
              );
            },
            separatorBuilder: (context, index) {
              return SizedBox(height: .01.h);
            },
            itemCount: savedEvents.length,
          ),
        )
      : Text("There are no events");
     }
   },
  );
  import 'dart:convert';

import 'package:flutter/material.dart';

class Eventdata {
static const String collectionName="eventNamecollection";
 String? eventID;
 final String eventtitle;
final String description;
final DateTime eventdate;
final String eventimage;
final String eventcategory;
bool isfav;

 Eventdata(
  {this.eventID ="",
  required this.eventtitle,
    required this.description,
    required this.eventdate,
    required this.eventimage,
    required this.eventcategory,
  this.isfav = false}); // function to send this event details to 
  firebase and another one to get events

  Map<String, dynamic> tofirestore() {
   return {
  "eventId": eventID,
  "eventTitle": eventtitle,
  "eventImage ": eventimage,
  "description": description,
  "eventCategory": eventcategory,
  "eventDate" : eventdate.millisecondsSinceEpoch,
  "isfav": isfav
  };
  }

  factory Eventdata.fromFirestore(Map<String, dynamic> json) {
  return Eventdata(
  eventID: json["eventId"] ?? "",  // Default to empty string if 
 null
  eventtitle: json["eventTitle"] ?? "",  // Default to empty string 
if null
  description: json["description"] ?? "",  // Default to empty 
string if null
  eventcategory: json["eventCategory"] ?? "",  // Default to empty 
 string if null
  eventimage: json["eventImage"] ??"",  // Default to empty string 
 if null
  eventdate: json["eventDate"] != null
      ? DateTime.fromMillisecondsSinceEpoch(json["eventDate"])
      : DateTime.now(),  // Default to current time if null
   isfav: json["isfav"] ?? false,  // Default to false if null
   );
  }




   }

I'm building a Flutter application where I store event data in Firebase, including an image URL for each event. The image paths are stored as strings in Firestore under the eventImage field (e.g., "assets/images/event.png"). However, when I attempt to display the image using the AssetImage widget, I am getting the following error: Unable to load asset: ""

   StreamBuilder<QuerySnapshot<Eventdata>>(
    stream: FirebaseFunctions.getstreamdata(),
      builder: (context, snapshot) {
      if (snapshot.hasError) {
      return Column(
        children: [
      Text("Something went wrong"),
      SizedBox(height: 12),
      IconButton(
        onPressed: () {},
        icon: Icon(
          Icons.refresh,
          color: Colors.blue,
        ),
      ),
    ],
      );
    }
    if (snapshot.connectionState == ConnectionState.waiting) {
     return const Center(
     child: CircularProgressIndicator(
      color: colorpallete.parimary,
     ),
     );
    } else {
     List<Eventdata> savedEvents = snapshot.data!.docs.map(
    (element) {
      var event = element.data();
      print("Fetched Event: ${event.eventimage}");
      return event;
    },
     ).toList();

    return savedEvents.isNotEmpty
      ? Expanded(
          child: ListView.separated(
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              var event = savedEvents[index];
              return event_card(
                eventddatamodel: event,
              );
            },
            separatorBuilder: (context, index) {
              return SizedBox(height: .01.h);
            },
            itemCount: savedEvents.length,
          ),
        )
      : Text("There are no events");
     }
   },
  );
  import 'dart:convert';

import 'package:flutter/material.dart';

class Eventdata {
static const String collectionName="eventNamecollection";
 String? eventID;
 final String eventtitle;
final String description;
final DateTime eventdate;
final String eventimage;
final String eventcategory;
bool isfav;

 Eventdata(
  {this.eventID ="",
  required this.eventtitle,
    required this.description,
    required this.eventdate,
    required this.eventimage,
    required this.eventcategory,
  this.isfav = false}); // function to send this event details to 
  firebase and another one to get events

  Map<String, dynamic> tofirestore() {
   return {
  "eventId": eventID,
  "eventTitle": eventtitle,
  "eventImage ": eventimage,
  "description": description,
  "eventCategory": eventcategory,
  "eventDate" : eventdate.millisecondsSinceEpoch,
  "isfav": isfav
  };
  }

  factory Eventdata.fromFirestore(Map<String, dynamic> json) {
  return Eventdata(
  eventID: json["eventId"] ?? "",  // Default to empty string if 
 null
  eventtitle: json["eventTitle"] ?? "",  // Default to empty string 
if null
  description: json["description"] ?? "",  // Default to empty 
string if null
  eventcategory: json["eventCategory"] ?? "",  // Default to empty 
 string if null
  eventimage: json["eventImage"] ??"",  // Default to empty string 
 if null
  eventdate: json["eventDate"] != null
      ? DateTime.fromMillisecondsSinceEpoch(json["eventDate"])
      : DateTime.now(),  // Default to current time if null
   isfav: json["isfav"] ?? false,  // Default to false if null
   );
  }




   }
Share Improve this question edited Mar 21 at 16:00 seef ayman asked Mar 21 at 0:20 seef aymanseef ayman 12 bronze badges 2
  • and when i chech it in firebase database i found the correct path is passed – seef ayman Commented Mar 21 at 0:22
  • Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Mar 21 at 6:39
Add a comment  | 

2 Answers 2

Reset to default 1
  1. Confirm the images are actually in your assets/images/ folder.
  2. Test by using the path directly with AssetImage. (Without fetching from firestore)
  3. Specify assets under pubspec.yaml.
flutter:
  assets:
    - assets/
    - assets/images/
  1. If the above does not work, your image might have issues. Example could be the image extension being changed without a proper conversion

Instead of storing "assets/images/event.png" in Firestore, upload your images to Firebase Storage and store the download URLs in Firestore.

"https://firebasestorage.googleapis/v0/b/your_project.appspot/o/event.png?alt=media"

Imagework(event.eventimage)

StreamBuilder<QuerySnapshot<Eventdata>>(
  stream: FirebaseFunctions.getstreamdata(),
  builder: (context, snapshot) {
    if (snapshot.hasError) {
      return Column(
        children: [
          Text("Something went wrong"),
          SizedBox(height: 12),
          IconButton(
            onPressed: () {},
            icon: Icon(
              Icons.refresh,
              color: Colors.blue,
            ),
          ),
        ],
      );
    }

    if (snapshot.connectionState == ConnectionState.waiting) {
      return const Center(
        child: CircularProgressIndicator(
          color: colorpallete.parimary,
        ),
      );
    }

    List<Eventdata> savedEvents = snapshot.data!.docs.map((element) {
      var event = element.data();
      print("Fetched Event: ${event.eventimage}");
      return event;
    }).toList();

    return savedEvents.isNotEmpty
        ? Expanded(
            child: ListView.separated(
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                var event = savedEvents[index];
                return event_card(
                  eventddatamodel: event,
                  imageWidget: event.eventimage.startsWith('http')
                      ? Imagework(event.eventimage) // For Firebase URLs
                      : Image.asset(event.eventimage),   // For asset images
                );
              },
              separatorBuilder: (context, index) {
                return SizedBox(height: .01.h);
              },
              itemCount: savedEvents.length,
            ),
          )
        : Text("There are no events");
  },
)

本文标签: Unable to load image from Firebase Storage in FlutterquotUnable to load asset quotquot errorStack Overflow