Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter use network images protected with Basic Auth

We have a main site that holds all the user's uploads which are usually just profile pictures.

The folder with all the uploads is protected with nginx and the basic auth. So you need to enter a username and password to access it.

Question

How can this be done using Flutter cached_network_image? Or something similar. We don't want to physically store the images on the device.


1 Answers

You can use httpHeaders property on CachedNetworkImage widget to pass custom auth headers.

CachedNetworkImage(
  imageUrl: ...,
  imageBuilder: ...,
  placeholder: (context, url) => CircularProgressIndicator(),
  errorWidget: (context, url, error) => Icon(Icons.error),
  httpHeaders: {
    "Authorization": "Basic $token",
  }
),
like image 100
Pionix Avatar answered Mar 18 '26 21:03

Pionix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!