More
How to Use Flutter Completers with a Backend on Back4app
11 min
introduction futures and streams are the way to go in any type of asynchronous operation however, sometimes both are not enough if you want more advanced control over futures , then use a completer it is a tool through which you can complete a future programmatically in a way, this provides better control over asynchronous operations this tutorial will help you work through using flutter completers in an application that interacts with a backend on back4app by the end of this tutorial, you will have learned how to use completers in a flutter application to manage asynchronous tasks and integrate these tasks with a provided backend service from back4app we are going to create a very simple app which gets data back from a back4app backend using a completer for controlling the flow of the application prerequisites to complete this tutorial, you will need a back4app account sign up for a free account at back4app com https //www back4app com flutter development environment set up on your local machine follow the flutter installation guide https //flutter dev/docs/get started/install if you haven't set it up yet basic knowledge of dart and asynchronous programming in flutter if you need a refresher, check out the flutter async programming guide https //dart dev/codelabs/async await step 1 – setting up your back4app backend first, let's set up a simple backend on back4app that our flutter application will interact with log in to your back4app account and create a new project set up a new parse class named task , which will store tasks that our flutter app will fetch add the following columns to the task class name (string) the name of the task status (boolean) the status of the task (completed or not) add a few sample tasks in the back4app database to test with for example task 1 name = "complete flutter tutorial", status = true task 2 name = "start new project", status = false your back4app backend is now ready to be accessed by the flutter app step 2 – creating a flutter project next, we'll create a new flutter project open your terminal or command prompt run the following command to create a new flutter project flutter create completer demo navigate to the project directory cd completer demo 2\ open the project in your preferred code editor (e g , vs code, android studio) step 3 – adding the parse sdk and configuring the app now, let's add the necessary dependencies to interact with back4app open pubspec yaml and add the following dependencies dependencies flutter sdk flutter parse server sdk flutter ^3 0 0 2\ run flutter pub get to install the dependencies 3\ in lib/main dart , import the parse sdk import 'package\ flutter/material dart'; import 'package\ parse server sdk flutter/parse server sdk flutter dart'; import 'dart\ async'; 4\ initialize parse in the main function void main() async { widgetsflutterbinding ensureinitialized(); const keyapplicationid = 'your app id'; const keyclientkey = 'your client key'; const keyparseserverurl = 'https //parseapi back4app com'; await parse() initialize(keyapplicationid, keyparseserverurl, clientkey keyclientkey, autosendsessionid true); runapp(myapp()); } replace 'your app id' and 'your client key' with your actual back4app credentials step 4 – using completers to fetch data asynchronously now, let's use a completer to fetch data from the back4app backend and control when the data is available for use in the ui in lib/main dart , create a new class that will fetch tasks from back4app using a completer class taskmanager { final completer\<list\<parseobject>> completer = completer(); future\<list\<parseobject>> get tasks => completer future; taskmanager() { fetchtasks(); } void fetchtasks() async { querybuilder\<parseobject> query = querybuilder\<parseobject>(parseobject('task')); final parseresponse response = await query query(); if (response success && response results != null) { completer complete(response results); } else { completer completeerror('failed to load tasks'); } } } this class initializes a completer , starts fetching data, and completes the completer when the data is available 2\ in the myapp widget, use the taskmanager to fetch and display tasks class myapp extends statelesswidget { @override widget build(buildcontext context) { return materialapp( home scaffold( appbar appbar(title text('flutter completer demo')), body futurebuilder\<list\<parseobject>>( future taskmanager() tasks, builder (context, snapshot) { if (snapshot connectionstate == connectionstate waiting) { return center(child circularprogressindicator()); } else if (snapshot haserror) { return center(child text('error ${snapshot error}')); } else if (!snapshot hasdata || snapshot data! isempty) { return center(child text('no tasks found ')); } else { return listview\ builder( itemcount snapshot data! length, itembuilder (context, index) { final task = snapshot data!\[index]; return listtile( title text(task get\<string>('name') ?? 'no name'), subtitle text('completed ${task get\<bool>('status') ? 'yes' 'no'}'), ); }, ); } }, ), ), ); } } 3\ run your flutter app you should see a list of tasks fetched from your back4app backend, with their names and completion status conclusion in this tutorial, you learned how to use flutter completers to manage asynchronous operations and control the flow of data fetching in your application by integrating with back4app, you created a simple yet powerful backend for your flutter app, allowing you to fetch and display data dynamically this approach can be extended to handle more complex scenarios, such as user authentication, data manipulation, and more for more information on using flutter with back4app, check out the back4app flutter documentation https //www back4app com/docs/flutter/overview happy coding!