-1

I'm trying to return the exif from the file that I can print directly from within the function, but whenever I use return it sends a Future. Looks like await doesn't work.

import 'dart:io';
import 'package:exif/exif.dart';

consultaDataArquivo(var pathArquivo) async {
  final dadosDoArquivo = await pathArquivo.readAsBytesSync();
  var dadosExif = await readExifFromBytes(dadosDoArquivo);
  return dadosExif;
}

main() {
  print(consultaDataArquivo(File('/home/rcaliman/Downloads/_DSC8982.JPG')));
}
  • Because `consultaDataArquivo` is an asynchronous function (you marked it as `async`). Async functions always return `Future` – Alex Usmanov Jul 20 '22 at 03:58
  • Another note. All methods, in Dart, should specify what it returns since if nothing is specified, it means `dynamic` and can therefore be anything (including nothing). Since the type system cannot really help us with `dynamic` types, it is best to specify types even `void` if that is the case. In this case, `readExifFromBytes` returns `Future>` so your method should return the same type. Yes, including the `Future` part since your method are marked `async` and therefore always returns a `Future`. – julemand101 Jul 20 '22 at 04:12
  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Jul 20 '22 at 07:29

1 Answers1

2

because you are not awaiting for the function to return the result.

consultaDataArquivo(var pathArquivo) async {
  final dadosDoArquivo = await pathArquivo.readAsBytesSync();
  var dadosExif = await readExifFromBytes(dadosDoArquivo);
  return dadosExif;
}

main() async{
  print(await consultaDataArquivo( File('/home/rcaliman/Downloads/_DSC8982.JPG')));
}

for more officail docs: https://dart.dev/codelabs/async-await

Ruchit
  • 2,137
  • 1
  • 9
  • 24