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
2 Answers
Reset to default 1- Confirm the images are actually in your
assets/images/
folder. - Test by using the path directly with
AssetImage
. (Without fetching from firestore) - Specify assets under
pubspec.yaml
.
flutter:
assets:
- assets/
- assets/images/
- 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 Flutter - "Unable to load asset: "" error - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744377174a2603317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论