Flutter Login with REST API and SQLITE using Blocs.

Amartya Gaur
Flutter Community
Published in
7 min readMay 21, 2020

--

What is this post about?

This post is about creating a flutter login working with an API, I made the API in Django with the help of DRF (please read them: post #1, post #2 before this post in case you want to develop the API as well). This post will be about the flutter application that will function with that API. We are going to create an application to allow a user to log in and log out.

You can also view the source code at this repo.

The output

Pre-requisites

To follow along with the post, you need to have flutter installed (refer: https://flutter.dev/docs/get-started/install) along with visual studio code and the extension for flutter (refer: https://flutter.dev/docs/development/tools/vs-code) and bloc (refer: https://marketplace.visualstudio.com/items?itemName=FelixAngelov.bloc).

Let’s Code

Begin with creating a project (it would be better to use the vscode extension for the same) called bloc_login (Ctrl + shift + p, type flutter and you will get an option to create a new project).

Create a new flutter project in VS-Code

Next, we will create certain folders in order to separate different components of our code. Create the following directories in bloc_login/

  1. api_connection (will contain the code to communicate with our API)
  2. common (Will contain utility widgets like loading indicator)
  3. dao (Will contain a helper file in order to communicate with our SQLite database)
  4. model (Will contain schema of our database)
  5. repository (Will act as a bridge between our API, blocs, and database)
  6. splash (Will contain splash screen code)
  7. login (Will contain login form and other details pertaining to it)
  8. database (Will contain files in order to initialize DB and create the table to start inserting data)
  9. home (Will contain our main UI)
  • You can refer to this page in order to get a basic understanding of how flutter blocs work. To understand what blocs actually are you can give this page a read. In short, they are a way to map our incoming events to states which flutter will consume in order to rebuild UI in accordance.
  • Add dependencies. We are going to use Equatable, flutter_bloc, sqflite and http packages, thus modify your pubspec.yaml file and include the following dependencies:
Dependencies (pubspec.yaml)

Note: I have just shown the dependencies part where you need to add the files for dependencies.
Just saving the file in VScode will automatically trigger the command to get these packages but, for reference, run the following command from your bloc_login/ directory:

flutter pub get

Let us start by making our database helper thus, create a file user_database.dart inside your bloc_directory/database directory and add the following code to it:

Database Provider

Here we return the opened database as the database variable in our DatabaseProvider class. Thus, dbProvider will initialize the DatabaseProvider class and will have a get database function that will return the opened database.

We can use the onUpgrade function in case we need to migrate some changes between different versions. Also our final userTable variable is used to store the table name for our database.

Let us next create our model for the user that corresponds to the table we just created. Thus, create a file bloc_login/model/user_model.dart and add the following code to it:

User Model

Here we also defined the factory function fromDatabaseJson which will return the user as a JSON object and the toDatabaseJson which will be responsible for converting the incoming JSON object to a database object which can be stored to our database.

Let us add API functionality thus create the file bloc_login/api_connection/api_connection.dart and add the following code to it:

API Connection

I have used the above URL because I named my app home_hub for Heroku, you need to replace that with the URL you got from the previous post. The rest can remain the same.

We just have a helper function here in order to get token for any particular user and throw an error in case the details are not right.

Let us add a model in order to deal with requests to API and subsequent responses. Create a file bloc_login/model/api_model.dart and add the following code to it:

API model

We are going to use this model in order to just get the username and password from the user and then send them to the server. The Token class gets the result and gives us the string from the JSON object we receive from the server.

Let us create a dao object to provide some basic operations for User model and the database, create a file bloc_login/dao/user_dao.dart and add the following code to it:

User DAO

This basically provides us with methods to create a user / delete a user and search if a user exists or not. Since our app will provide login facilities to only one user at max, we are forcing the user to be created with id 0. Thus, we can always check for the existence of a user by checking the first entry in our database. The best part is that we are just storing the username and token and not the password entered. The password is just used to send a request to the server and we never store than anywhere else. There is an alternative here, you can always use flutter_secure_storage in order to store the token and use it but I wanted to use SQL as I want to further extend the application to include other functionalities.

Let us come to the logical implementation of our apps i.e. the blocs. I recommend using the vscode extension to create a new bloc (refer: here) although, you can always manually create the files. Create new bloc in bloc_login. directory and name it authentication_bloc. The extension will automatically create the bloc_login/bloc folder and create three files in it corresponding to the states, events, and the bloc for authentication_bloc. Modify your bloc_login/bloc/authentication_state.dart and add the following code:

Authentication States

Here we define four authentication states which are Uninitialized which can correspond to the state when the app is waiting to check if a user exists in our database or not, the loading state which can be a state when we are waiting for the app to store a token or delete a token, authenticated state corresponding to the successful login of a user and unauthenticated which corresponds to the user not being authenticated yet / logged out.

Let us define the events for our authentication bloc modify your bloc_login/bloc/authentication_event.dart and add the following code:

Authentication Events

This corresponds to three events that might occur, AppStarted which will notify the block to check if a user exists, LoggedIn which will be the event suggesting that the user has logged in successfully and LoggedOut which will tell that the user has logged out.

Now we need to implement our bloc that is map our events to states, modify your bloc_login/bloc/authentication_bloc.dart and add the following:

Authentication Bloc

Here we did the following:
- Initialized state to be AuthenticationUninitialized
- Yielded the AuthenticationAuthenticated state which corresponds to the existence of users in our database. This will help in the persistence of the state because the user will exist even after we clear our app from the ram.
- If the event was LoggedIn, we saved the user to the database by calling the persistToken function we defined.
- If the event was LoggedOut, we deleted the user from the database.

Enters UI, let us create our splash screen, create the file bloc_login/splash_page.dart and the following code to it:

Splash Page

We will also export this code in the file bloc_login/splash/splash.dart for easy access:

export 'splash_page.dart';

Let us also create our home page so that we can navigate users when they are logged in. Thus, create the file bloc_login/home/home_page.dart and add the following code:

Home Page

Here we are displaying Welcome and a button to Log Out a user, notice that we got our bloc provider from the build context (we will add the code for it in main.dart) and notified it that the LoggedOut event has occurred by adding that event on button press.

We also need to create a login bloc to handle our log in states and events. Thus, make another bloc called login using the extension in the bloc_login/login directory we created and add the following code in the bloc_login/login/bloc/login_states.dart

Login States

Here the LoginInitial state is the initial state for our login form, LoginLoading is the state when our form is validating the credentials and LoginFaliure indicates that the login attempt was unsuccessful.

Let us create our login events, modify your bloc_login/login/login_event.dart file and add the following code:

Login Events

Let us also add the login to map the events to state in bloc_login/login/login_bloc.dart. Add the following code in this file:

Login Bloc

Let us create our login form. Create a file bloc_login/login/login_form.dart and add the following code:

Login Form

Also, we will create a file bloc_login/login/login_page.dart and add the following code:

Login Page

We are all set, now all that is needed to be done is to create our bloc_login/main.dart file and also create a loading indicator. Let us first create the loading indicator, create bloc_login/common/loading_indicator.dart and add the following code:

Loading Indicator

We will also export it from bloc_login/common/common.dart for easy access:

export './loading_indicator.dart';

Finally, add the following code in bloc_login/main.dart

Main

I have added the BlocDelegate and overridden the functions for debugging purposes. I know this is a slightly long post but I thought it will be good to include the whole application here for reference.

You can also view the source code at this repo.

Links to previous posts:
Flutter signup/login application with Django backend #1
Flutter signup/login application with Django backend #2

Please feel free to drop a message at Ohuru in order to avail various development services offered by my company.

--

--

Amartya Gaur
Flutter Community

Amartya Gaur I am a machine learning enthusiast and an experienced Django developer