my networkimage can read images from other links, but when it’s from firestore it can’t read, I don’t know why, I’ve already checked and the link that the console shows is working normally, the firebase rules are also public, I’m running out of options
code that shows the photo
usuario.isLoggedIn
? FutureBuilder<String?>(
future: _loadUserAvatar(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircleAvatar(
maxRadius: 40,
backgroundColor: Colors.white,
child: CircularProgressIndicator(
color: colorPrimary,
), );
} else if (snapshot.hasData && snapshot.data != null) {
return CircleAvatar(
backgroundImage: NetworkImage(snapshot.data!),
maxRadius: 40,
backgroundColor: Colors.white,
);
} else {
return CircleAvatar(
backgroundImage: const AssetImage('icons/user.png'),
maxRadius: 40,
backgroundColor: Colors.white,
);
}
},
)
: CircleAvatar(
child: Icon(
Icons.person,
color: colorPrimary,
size: 50,
),
maxRadius: 40,
backgroundColor: Colors.white,
),
error of log:
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
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/comp``...-8161d.firebasestorage.app/o/...",
scale: 1.0)
Image key:
NetworkImage("``https://firebasestorage.googleapis.com/v0/b/comp``...-8161d.firebasestorage.app/o/...",
scale: 1.0)
upload image code:
Future<String?> _uploadAvatarImage(Uint8List imageBytes) async {
try {
final img.Image? originalImage = img.decodeImage(imageBytes);
if (originalImage == null) { throw Exception("Falha ao decodificar a imagem");
} final img.Image resizedImage = img.copyResize(originalImage,
width: 256,
height: 256
);
String fileExtension;
String contentType;
Uint8List resizedBytes;
if (imageBytes[0] == 0xFF && imageBytes[1] == 0xD8 && imageBytes[2] == 0xFF) {
fileExtension = '.jpg';
contentType = 'image/jpeg';
resizedBytes = Uint8List.fromList(img.encodeJpg(resizedImage));
} else if (imageBytes[0] == 137 && imageBytes[1] == 80 && imageBytes[2] == 78) {
fileExtension = '.png';
contentType = 'image/png';
resizedBytes = Uint8List.fromList(img.encodePng(resizedImage));
} else {
throw Exception("Formato de imagem não suportado");
}
final String userId = FirebaseAuth.instance.currentUser!.uid;
final Reference storageRef = FirebaseStorage.instance
.ref()
.child("user_avatars/$userId$fileExtension");
final SettableMetadata metadata = SettableMetadata(contentType: contentType);
await storageRef.putData(resizedBytes, metadata);
final String imageUrl = await storageRef.getDownloadURL();
return imageUrl;
} catch (e, stacktrace) { print("Erro ao fazer upload da imagem: $e"); print("Stacktrace: $stacktrace");
return null;
}
}
i tried everything, reading the log is correct, I use it in the browser and the image appears perfectly, I’ve been stuck with this problem for 3 days, I tried to change the image’s metadata and it didn’t help, please someone help me
2
Answers
The problem was in Google Cloud's CORS, try setting cors.json to the following code:
I had difficulty changing CORS, directly in the Google Cloud console it didn't work, I had to download a tool called firebase admin, create a node project, download the json key from my firebase and run this code:
you have to have node npm, you have to have firebase sdk and firebase admin sdk.
I don't remember exactly what the commands were but I remember that I just ran this code. Another quick but not recommended alternative that I found was to use the HTML renderer, but it is slow and not worth it for anyone who is actually creating a website, just for testing, etc. To use the renderer, just use this command in the terminal:
for run:
for build:
Translate the commented lines into Brazilian Portuguese, as I am Brazilian, thanks
Salve bro,
I have a reusable PickImage class in my app that I use. It might be overkill but I’ll share it with you.
And then I make another class like this
Then you can call it in your UI and add any customization you want, like
Let me know if that helps at all.