Dart for your Backend: Part-2

Aswin Gopinathan
3 min readNov 27, 2022

--

In my last blog Getting started with Dart server applications , you learned about the shelf package, what a Pipeline and a Middleware is, and you also saw how you can build your own handler and connect it to an endpoint.

Well, in this blog, you will learn how you can use a local json data-file and build an API that displays its content.

Let’s create a file, and name it assets/data.json with the following content :

{
"languages": {
"dart": "https://dart.dev",
"swift": "https://www.swift.org",
"kotlin": "https://kotlinlang.org"
}
}

Note: In a Pure Dart application, you don’t have to update the pubspec.yaml file to add the dependency of the assets folder.

So, what we are gonna do is, we will be hosting this data in our local server application.

We will be adding the code to the server.dart file for now, later in my future blogs, i will show a better way how you can separate logics into different files using the shelf_router package.

Let’s start with a function that reads the data.json file content and converts it into a String.

Future<String> parseLocalFile() async {
File file = File('assets/data.json');
String fileContent = await file.readAsString();
return fileContent;
}

Next, we need two things:

  • An endpoint to expose this data to the client.
  • A Handler function to return data when the client hits the above endpoint.

Let’s start with defining the handler function:

Future<Response> _dataHandler(Request request) async{
final fileData = await parseLocalFile();
return Response.ok(
fileData,
headers: {
'Content-Type': 'application/json',
},
);
}

I have named the handler function as _dataHandler in which we get the fileData in the form of String and pass it to the body of the Response.ok() constructor.

We have also defined the headers where we are telling the client that the Response is of type application/json .

Next, let’s define the endpoint and attach the handler to it:

final _router = Router()
..get('/', _rootHandler)
..get('/echo/<message>', _echoHandler)
..get('/echo/<num1>/<num2>', _ourHandler)
// Add this following line.
..get('/data', _dataHandler);

We have used the endpoint /data . If everything goes well, we will be able to see the json data when we hit the endpoint /data .

Run the server application, and hit the endpoint /data . You should be able to see the json data on your page:

I am using the chrome extension json-formatter , which formats the output when it is of the type JSON. This extension is the best way to find out if you are sending data in the JSON format.

Remember the line Content-Type: application/json which we passed in the headers . This defines the output type of your Response.

— -

Well, with that we have come towards the end of this article.

In my upcoming blog, i will talk about how you can work with the shelf_router package to compose your custom request handlers and clean up your project structure.

Till then, feel free to reach out to me on the following platforms :

--

--

Aswin Gopinathan
Aswin Gopinathan

Written by Aswin Gopinathan

Software Engineer @PhonePe (iOS)

No responses yet