final imageFile = await File(path); doesn't create a file


I am using the camera to take a picture and save the picture on phones memory to later send it to AWS. I can take a picture and the path for the picture is there, but I am failing to create a file from the path using:

final imageFile = await File(path);

Why is the file not being created here? I always get ‘Image file does not exist’ on debug console.

    import 'dart:io';
    import 'package:camera/camera.dart';
    import 'package:flutter/material.dart';
    import 'package:path/path.dart';
    import 'package:path_provider/path_provider.dart';

    class CameraPage extends StatefulWidget {
      // 1
      final CameraDescription? camera;
      // 2
      final ValueChanged? didProvideImagePath;

      CameraPage({Key? key,, this.didProvideImagePath})
          : super(key: key);

      State<StatefulWidget> createState() => _CameraPageState();

    class _CameraPageState extends State<CameraPage> {
      CameraController? _controller;
      Future<void>? _initializeControllerFuture;

      void initState() {
        // 3
        _controller = CameraController(!, ResolutionPreset.medium);
        _initializeControllerFuture = _controller?.initialize();

      Widget build(BuildContext context) {
        return Scaffold(
          body: FutureBuilder<void>(
            future: _initializeControllerFuture,
            builder: (context, snapshot) {
              // 4
              if (snapshot.connectionState == ConnectionState.done) {
                return CameraPreview(this._controller!);
              } else {
                return Center(child: CircularProgressIndicator());
          // 5
          floatingActionButton: FloatingActionButton(
              child: Icon(, onPressed: _takePicture),

      // 6
      void _takePicture() async {
        try {
          await _initializeControllerFuture;

          final tmpDirectory = await getTemporaryDirectory();
          final filePath = '${}.jpg';
          final path = join(tmpDirectory.path, filePath);

          await _controller?.takePicture();


          final imageFile = await File(path);

          if (imageFile.existsSync())
            print('Image file exists');
            print('Image file does not exist');
        } catch (e) {

      // 7
      void dispose() {


takePicture method will return taken image as XFile, you can use the XFile path to create a File with taken Image path like below code.

  File? imageFile;
  XFile? xFile = await _controller?.takePicture();
  if (xFile != null) {
    imageFile = File(xFile.path);

  if (imageFile != null && imageFile.existsSync()) {
    print('Image file exists');
  } else {
    print('Image file does not exist');

Answered By – MSARKrish

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *