DEV Community

loading...
Cover image for Working with Future objects in Dart & Flutter

Working with Future objects in Dart & Flutter

theimpulson profile image Aayush Gupta Updated on ・4 min read

If you have just started working in Dart, you must have come across Future objects, or if not, you will. A lot of built-in dart methods & functions return or work with Future object. So, you might ask, what is this Future? Why does this X method returns a Future object? The answer to these questions is quite simple if you focus on the naming of the object itself.

A Future is something that will happen, it has not happened now, but it will happen and that’s inevitable. That’s what a Future object in dart implies. It’s not ready right now, this is not the answer we want, the answer we want will be given sometime in the Future.

Let’s explain it by an example. Suppose you (developer) are playing soccer in a playground with your friends (dart methods). You ask (makes a query), “would it rain today”? to your friend Aayush (a dart method). Now, what do you think would happen? Aayush (method) would maybe look up in the sky for clouds, wind, and other things and after a while give you an answer (return an object). This object can be either an answer to your query (Yes or No) or an error (I don’t know).

A Future can have only 2 outcomes. An answer that you need or an error. To arrive at one of those 2 outcomes, it needs x amount of time where the value of x is unknown.

So, if your function/method returns a Future object when you use it, it means that it hasn’t finished executing that specific piece of code yet which can give the answer you want, but you queried for an answer beforehand. So, you might think, “So, all I need is to make dart understand that it needs to wait before asking for an answer from that specific function?”. Yes, that’s exactly what you need to do. Now the question arises, how can you do that? That’s where “async” and “await” comes into play.

async” is used to mark a function that is asynchronous where “await” is used to wait for a specific piece of code to finish its execution and return a result.

In case you are unfamiliar with the concept of asynchronous and synchronous programming, this question on StackOverflow is a good read to explain what these two are and what are the differences between them: Asynchronous vs Synchronous Programming.

If you are ever, writing a function that works with Future objects, you must mark it as “async” and use “await” in front of the code that returns a Future object, for it to work properly.

Not everything is clear without practical examples, is it? Let me show you a really simple example that would make it more clear.

Create a dummy file named test.log in the same directory & try to run this piece of code below.

import ‘dart:io’; // Import dart’s built-in io library  
main() {  
  final file = File(‘test.log’); // Create an instance of File class  
  print(file.lastAccessed()); // Print file’s last accessed time  
}
Enter fullscreen mode Exit fullscreen mode

Now, what got printed on the screen? is this what you expected? The answer is No. We expected it to print a Date and Time when the file was last accessed but what got printed was the Instance of ‘Future’. This happened because dart didn’t wait for the method to finish its execution and queried for an answer. Now let’s fix this by using “async” and “await” and see the result.

import ‘dart:io’; // Import dart’s built-in io library  
main() async { // Mark function as asynchronous  
  final file = File(‘test.log’); // Create an instance of File class  
  print(await file.lastAccessed()); // Wait & print the result  
}
Enter fullscreen mode Exit fullscreen mode

Now, did you get the result? The answer should be hopefully, Yes. Here as you can see, we marked our main() function as async and used the await keyword in front of the method which was returning a Future object. This resulted in a proper working piece of code.

An asynchronous function always returns a Future object. Hence, due to uncertainty of the time taken to complete the function, you cannot use it during init stage of a Flutter app.

So, in case you want to use a Widget that uses a function that returns a Future object in your Flutter app as the very first Widget (maybe a List View which contains your SQLite table data), you should use FutureBuilder class.

That’s all there is to with the Future, async, and await. I hope this article was able to clear your doubts and now you won’t be scared when a method/function returns a Future object.

Links to some nice articles on the same topic:

Discussion (0)

pic
Editor guide