Make Flutter app with Clojure ?
Introduction
We need to talk about ClojureDart, this allows us to write Flutter app and Dart program with Clojure language. This can be a real game changer for all Flutter developers out there.
We will make a quick example to show the potential of ClojureDart for Flutter.
Before starting you will need to install a few things to be able to start a project. Take a look at the quick start for the Flutter app here.
After the installation you should be able to start the project with the command clj -M:cljd flutter
.
Building an example app
We will create a simple list of items fetched from an API, and a second view with some details to it.
After the installation you should have a file src/acme/main.cljd
with inside:
As we can see, we can use ["package:flutter/material.dart" :as m]
to import a Dart package and use it like m/Scaffold
or every widget in material package.
The syntax can be hard to understand because we are on a functional paradigm here, if you are having some trouble take a look at the official documentation of Clojure. And this book (in free access), to see more about Clojure.
First, we will make a file to handle all the API calls. Create the src/api/albums.cljd
inside it paste this:
There are a few things to understand here.
First, the ns
(for namespace), will tell "where" is the file api/albums.cljd
so that after it we can see api.albums
in the folder where the file is. This will allow us to call the functions outside this file (will see later).
We can see some required packages like http
from Dart. To use it you will need to flutter pub add http
before building the app.
The second thing is the first function called base-url is a def function to define an object, here a simple string to get the base URL of the API. You can see this function is private via ^:private
, meaning you can use it only in this file.
Now let’s talk about the get-album
function, the function can take 0 arguments []
or one argument [page]
. This will fetch a list of albums from this API https://jsonplaceholder.typicode.com/albums
.
And if you pass a number argument this will get only one album, https://jsonplaceholder.typicode.com/albums/1
for example.
We will see later how to use it but we have done the API part.
Now back to the main.cljd
, remove everything, and paste this code:
As you can see to import the API file we just created we require [api.albums :as api]
. The api.albums
is the namespace we define before.
Now after this paste:
This code will create a simple material app with Scaffold and call the /albums
with the function (api.albums/get-album)
like you can see without parameters. We :watch
it because (api.albums/get-album)
return a Future. More information about :watch
and others ClojureDart here .
Then we use the response with [{sc .-statusCode body .-body} ^http/Response response]
to get the http code and body.
With the case
function we can check the http code and create a list of items with (build-list-items (c/json.decode body))
when the code is 200 or if not a (m/Text (str "Something wrong happened, status code: " sc))
function to return an error message widget.
And return a (m/CircularProgressIndicator)
function when loading.
Now we will define the build-list-items
function before the main
:
This private function defn-
took an argument [body]
, the list of albums. With the ListView.builder
we create the list and a simple ListTile
with an onTap
callback to navigate to a detail view (album-detail/view album)
and we pass a second argument to the navigate
function (str "/album-detail/" (get album "id”))
to name our view.
Now the navigator part, paste this before the build-list-items
:
Simple navigation push with the widget builder page
and the name
the route name.
Subscribe to Etienne Théodore
And finally, we will create the album_detail.cljd
file in the folder src/pages/
:
To use it in the main you will need to import it via the required like so:
Now we can run the application via clj -M:cljd flutter
.
Well done the example is completed!
Conclusion
We have made a quick example application to fetch from an API a list of items and made a List to scroll these items. Then a detailed view to see more about the item.
I enjoyed making some Clojure, it’s a new language and paradigm for me. But using it to make a Flutter app was fun! I think ClojureDart could be nice for production apps, but we still need to talk about unit testing, and architecture maybe in a future article :) !
Like always take a look at the code here => https://github.com/Kiruel/fetch-data-list. Don’t forget to join the mailing list if you want to not miss any articles.
Subscribe to Etienne Théodore
Usefull links: