How can I bring an image from an api and show it in my app?

I believe you’re referring to storing images in Flutter using an API and a package called “image_picker” to select images from the device. Here’s an example of how you can achieve that:

Step 1: Import the necessary packages First, you need to import the http and image_picker packages in your Flutter app. Add the following dependencies to your pubspec.yaml file:

  http: ^0.13.3
  image_picker: ^0.8.3+3

Then, run flutter pub get to download and install the packages.

Step 2: Add Image Picker UI in your app Next, add UI components to your Flutter app to allow the user to select an image using the image_picker package. Here’s an example using a simple button to open the image picker dialog:

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImagePickerExample extends StatefulWidget {
  _ImagePickerExampleState createState() => _ImagePickerExampleState();

class _ImagePickerExampleState extends State<ImagePickerExample> {
  File? _image;

  // Function to open image picker dialog
  Future<void> _pickImage() async {
    final pickedImage =
        await ImagePicker().getImage(source:;
    if (pickedImage != null) {
      setState(() {
        _image = File(pickedImage.path);

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Image Picker Example')),
      body: Center(
        child: _image == null
            ? Text('No image selected')
            : Image.file(_image!),
      floatingActionButton: FloatingActionButton(
        onPressed: _pickImage,
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),

In the above code, we’re using the ImagePicker class from the image_picker package to open the image picker dialog and select an image from the device’s gallery. The selected image is then displayed on the screen using the Image.file widget.

Step 3: Upload the Image to API Once the image is selected using the image picker, you can upload it to your API using the http package or any other similar HTTP client library. Here’s an example of how you can use the http package to upload the selected image to an API endpoint:

import 'dart:async';
import 'dart:io';
import 'package:http/http.dart' as http;

// Function to upload image to API
Future<void> uploadImage(File image) async {
  final url = ''; // Replace with your API endpoint
  var request = http.MultipartRequest('POST', Uri.parse(url));
  request.files.add(await http.MultipartFile.fromPath('image', image.path));
  var response = await request.send();
  if (response.statusCode == 200) {
    print('Image uploaded successfully');
  } else {
    print('Failed to upload image');

In the above code, we’re creating a MultipartRequest using the http package and adding the selected image file as a multipart file to the request. The http package takes care of sending the request to the API endpoint with the image data.

Note: Please make sure to update the API endpoint URL and error handling based on your specific API implementation.

That’s it! You’ve now learned how to store and upload images to an API in Flutter using the image_picker and http packages.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x