How to Upload Images In Flutter ?

· 4 min read
How to Upload Images In Flutter
How to Upload Images In Flutter

Generally, Every Application requires a section from where the user can take an Image from a Phone Gallery or Camera and displays it. So in this article, We will go through How to Upload Images In Flutter?

Are you ready for the same ??

Get, Set, and Let’s Go !!!

How to Upload Images In Flutter?

Upload(File imageFile) async {    
    var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
      var length = await imageFile.length();

      var uri = Uri.parse(uploadURL);

     var request = new http.MultipartRequest("POST", uri);
      var multipartFile = new http.MultipartFile('file', stream, length,
          filename: basename(imageFile.path));
          //contentType: new MediaType('image', 'png'));

      var response = await request.send();
      print(response.statusCode); {

the namespace will look like below:

import 'package:path/path.dart';
import 'package:async/async.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
  • The easiest way to do this is by using http.
    import 'dart:io';
    import 'package:http/http.dart' as http;
    _asyncFileUpload(String text, File file) async{
       //create multipart request for POST or PATCH method
       var request = http.MultipartRequest("POST", Uri.parse("<url>"));
       //add text fields
       request.fields["text_field"] = text;
       //create multipart using filepath, string or bytes
       var pic = await http.MultipartFile.fromPath("file_field", file.path);
       //add multipart to request
       var response = await request.send();
       //Get the response from the server
       var responseData = await;
       var responseString = String.fromCharCodes(responseData);

We have a plugin called Dio. Code Snippet will look like below:

uploadFileFromDio(UserProfile userProfile, File photoFile) async {
    var dio = new Dio();
    dio.options.baseUrl = url;
    dio.options.connectTimeout = 5000; //5s
    dio.options.receiveTimeout = 5000;
    dio.options.headers = <Header Json>;
    FormData formData = new FormData();
    formData.add("user_id", userProfile.userId);

    if (photoFile != null &&
        photoFile.path != null &&
        photoFile.path.isNotEmpty) {
      // Create a FormData
      String fileName = basename(photoFile.path);
      print("File Name : $fileName");
      print("File Size : ${photoFile.lengthSync()}");
      formData.add("user_picture", new UploadFileInfo(photoFile, fileName));
    var response = await"user/manage_profile",
        data: formData,
        options: Options(
            method: 'POST',
            responseType: ResponseType.PLAIN // or ResponseType.JSON
    print("Response status: ${response.statusCode}");
    print("Response data: ${}");

Users can give a try to firstore_firebase using the below code snippet.

import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
      home: new MyHomePage(),

class MyHomePage extends StatefulWidget {
  _MyHomePageState createState() => new _MyHomePageState();

class _MyHomePageState extends State<MyHomePage> {
  File sampleImage;

  Future getImage() async {
    var tempImage = await ImagePicker.pickImage(source:;

    setState(() {
      sampleImage = tempImage;

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Image Upload'),
        centerTitle: true,
      body: new Center(
        child: sampleImage == null ? Text('Select an image') : enableUpload(),
      floatingActionButton: new FloatingActionButton(
        onPressed: getImage,
        tooltip: 'Add Image',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.

  Widget enableUpload() {
    return Container(
      child: Column(
        children: <Widget>[
          Image.file(sampleImage, height: 300.0, width: 300.0),
            elevation: 7.0,
            child: Text('Upload'),
            textColor: Colors.white,
            onPressed: () {
              final StorageReference firebaseStorageRef =
              final StorageUploadTask task =


In this article, we have been through How to Upload Images In Flutter? is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc. is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply