Rest API Calling Using GetX Snippet

Sayed
Rest API Calling Using Getx Snippet
4 min readJan 7, 2021

A quick boilerplate code for calling Rest API using Getx Snippet, which is for fast development.

In this article, we will see how GetX is working for Rest API, and most importantly, we will see how easily we can maintain all this stuff using GetX snippet. In this article, I actually describe GetX snippet and architecture. So it will be beneficial if you have good knowledge of GetX.

GetX Snippet

GetX snippet is actually a VsCode extension, making life easier for anyone who programs with Flutter using GetX. To that extent, it’s quick and easy to find snippets of code snippets, or even whole classes, generated with just a few keystrokes. We have come to make your development easy with Get!!! It’s just 35 snippets, with completely intuitive prefixes, so get out front and start using it so you don’t forget. Using this snippet, we can easily avoid boilerplate code and easily call the API and other stuff. If you want to know about the GetX snippet, check out this link, you will find something special GetX snippet.

Introduction

In this article, we will see how we get all the data from the server using Getx snippet and also how the POST method will work. So, first of all, I create a folder that maintains the structure. So I create five folders in the lib folder. Each folder description is given below.

Folder Structure
  1. Provider
    In this provider folder, we write all the code which actually provides the data, such as API, local database, and Firebase. A simple way we can say data provider may, for instance, be an API, local database, or firebase.
  2. Model
    Our classes or data models responsible for abstracting our objects.
  3. Repository
    The repository is actually negotiating between controller and data. Repositories actually depend on an entity and contain all its functions to request data from a local API or database. Our controllers won’t have to know where the data comes from, and if you need to, you can use more than one repository on a controller. From an API, we will have a repository for this API object where we will call all the corresponding API objects. But the controller doesn’t have to know where it comes from, the repository being a repository.
  4. Controller
    Binding
    The Binding class is a class that decouples dependency injection, while “binding” routes to the state manager and the dependency manager.
    This lets you know which screen is being displayed when a specific controller is used and knows where and how to dispose of it.
    Using the binding class, You can configure how dependencies are to be organized and remove a route from the stack, or when the widget is used for disposition or none of them.
  5. Views
    Views are nothing. It actually contains all the pages or screens for our project.

Ok, now our theory part is finished, and I will describe my code snippet and describe how we can easily call Rest API using the GetX snippet.

Description

  1. Provider
    Here we show you two methods one is GET, and another one is POST. We have a demo API for both methods. So when we try to sent data to the server, then we use this ApiClient class. Here you will see how I sent data to the server and how I get data from the server using GetX snippet. The most important thing is that all this code is actually predefined. When you add GetX snippet in VsCode, then create an API provider file and then write.

getprovider then press the enter button you will see the magic.

2. Model

The model folder contains all the model classes. Here we have two model classes, one for GET and another is for the POST. You can easily make the model for the responses, copy the JSON responses, and goto this link to generate the model class.

3. Repository

We knew about the repository, so we can easily access the entity for calling the API in this class. Using GetX snippet, we can easily find all the code. So, create the file and the write

getrepository then hit the enter button

4. Controller

As I told you about the controller in the upper section. So in this controller, we actually create a List that is observable(obs). So using this controller, we can easily find our data. Don’t worry, GetX snippet also gives you predefined code for controller, write

getcontroller then press the enter

Dependency management

Dependency management is so much easy in a flutter. We can easily maintain this management, write in one line code (In my HomePage, you will find the comment section because this is one way to manage the dependency management) and this binding. Using GetX snippet, you can easily find this binding, write

getbinding then press the enter

So, guys, I attach my Github link below so that you can find the boilerplate code for calling the Rest API using Getx Snippet.

--

--