Flutter image.network headers

Flutter Image.network with headers

When using Image.network in Flutter, you can pass additional headers to the network request using the headers parameter. This allows you to include custom headers like authentication tokens, user agents, etc. with the request.

    
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class MyImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Image.network(
      'https://example.com/image.jpg',
      headers: {
        'Authorization': 'Bearer your_token',
        'User-Agent': 'Your User Agent',
      },
    );
  }
}
    
  

In the above example, we include two headers – ‘Authorization’ and ‘User-Agent’, each having their respective values. You can add more headers as needed.

These headers will be sent with the image request to the provided URL. Make sure to replace 'https://example.com/image.jpg' with the actual URL of the image you want to load.

Leave a comment