Local Authentication in Flutter — Passcode
By: Anmol Gupta
Tags: Dart, Flutter, Mobile Apps, Mobile App Development, Flutter Widget

Demo Module:

Hello and welcome to my new blog on Passcode authentication. In this blog, we shall learn and explore how we can implement a simple local authentication using a passcode. This type of authentication is much secure and private also it is extremely fast. Another user how doesn't know your passcode can’t access your app content directly. This type of authentication removes the server-side errors and provides clean checkout to access the app.

Please give it a try by yourself and try to find out how we can implement this type of functionality with ease. Hopefully, we will require some pub packages as well so please try to find out by yourself which package might help to implement this functionality.

So what your thought on this? please share your approach on this functionality. Now here is mine, so let’s go……

The packages that we are going to use are:

We will use the provider package for state management.

package for state management. shared_preferences package us various methods to store our data inside our app locally and safely.

package us various methods to store our data inside our app locally and safely. get_it help us to register our services in a very simple and easy way.

State management technique used in this module

Before getting into the functionality part let's first learn the state management structure of the app.

We have created a file baseModel.dart that contains a BaseModel class that extends ChangeNotifier . In this file, we will write all the common methods that we will use throughout our app such as implementing circular progress indicator when and method is awaited.

that contains a class that extends In this file, we will write all the common methods that we will use throughout our app such as implementing circular progress indicator when and method is awaited. For each screen, we will create a separate file that will contain all the code that will handle all the methods that will provide the data for that particular screen from the internet server or local server. Each file will extends BaseModel in general ChangeNotifier .

in general . We will create a separate widget that we will use to access the provider methods or class.BaseView can only be used for the model class that extends BaseModel .

. Please go through the bellow blogs for a better understanding of this state management technique.

Let's start our code:

Create a baseModel.dart file that extends ChangeNotifier .

import 'package:flutter/material.dart';



class BaseModel extends ChangeNotifier {}

Create a sharedPrefs.dart file that will contain the passCode uploading and fetching code from the local store and this model will be used for the passcode check out screen.

Uploading the passCode value:

setPassCode(int passCodeValue) async {

SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

sharedPreferences.setInt('passCode', passCodeValue);

}

sharedPreferences is an object of SharedPreferences that will provide us access to all the methods. This method takes a passCodeValue int that will be stored under the ' passCode' key.

sharedPreferences.setInt('passCode', passCodeValue);

setInt method creates a passCode key in the app local storage and store the passCodeValue in it.

Fetching the passCode value:

First, create a variable that will store the fetched value of the passCode from the local server.

int _passCode;



int get passCode => _passCode;

passCode is a getter of _passCode that get and store the value of _passCode whenever it is changed.

getPassCode method is used to fetch the value of passCode . To get the value of the passCode key SharedPreferences provides us getInt method.

getPassCode() async {

SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

int code = sharedPreferences.getInt('passCode');

print("**************" + code.toString());

_passCode = code;

notifyListeners();

}

Full File:

import 'package:local_passcode_auth/baseModel.dart';

import 'package:shared_preferences/shared_preferences.dart';



class SharedPrefs extends BaseModel {

int _passCode;



int get passCode => _passCode;



setPassCode(int passCodeValue) async {

SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

sharedPreferences.setInt('passCode', passCodeValue);

//upload the passCodeValue }



getPassCode() async {

SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

int code = sharedPreferences.getInt('passCode');

//fetch the passCode value

print("**************" + code.toString());

_passCode = code;

notifyListeners();

//informs the passCode getter that the value has been changed and it will update the ui wherever it is used.

}

}

Regester all the models using the get_it

locator.dart file:

import 'package:get_it/get_it.dart';

import 'package:local_passcode_auth/sharedPrefs.dart';



import 'baseModel.dart';



final locator = GetIt.instance;



void setupLocator() {

locator.registerLazySingleton(() => BaseModel());

locator.registerLazySingleton(() => SharedPrefs());

}

initialize the setupLocator() method inside the main method

void main() {

setupLocator();

runApp(

MaterialApp(

debugShowCheckedModeBanner: false,

theme: ThemeData.dark(),

darkTheme: ThemeData(),

home: MyApp(),

),

);

}

baseView.dart file:

import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'baseModel.dart';

import 'locator.dart';



class BaseView<T extends BaseModel> extends StatefulWidget {

final Widget Function(BuildContext context, T model, Widget child) builder;

final Function(T) onModelReady;



BaseView({@required this.builder, this.onModelReady});



@override

_BaseViewState<T> createState() => _BaseViewState<T>();

}



class _BaseViewState<T extends BaseModel> extends State<BaseView<T>> {

T model = locator<T>();



@override

void initState() {

if (widget.onModelReady != null) {

widget.onModelReady(model);

}

super.initState();

}



@override

Widget build(BuildContext context) {

return ChangeNotifierProvider<T>.value(

//builder: (context) => model,

child: Consumer<T>(builder: widget.builder),

//notifier: model,

value: model,

);

}

}

checkoutPage.dart file:

This page is our main page that will contain a simple TextField that will take the passCode from the user and verify it with the fetched Passcode, if both the values are the same then we will navigate it to the HomePage .

Here we will require the sharedPrefs fetch and upload methods that we have created in sharedPrefs.dart file.

To use SharedPrefs class we need to use the BaseView class. BaseView class provides us builder , onModelReady property to build the builder of the Consumer and onModelReady will be used to initialize the methods or values inside the initState method.

Data Flow of this file:

When this Widget is called inside the init state model.setPassCode(1234) , model.getPassCode() will be called so that the passCode getter in the sharedPrefs.dart can get the value of the passcode. BaseView provides us onModelReady method to initialize the methods inside the initState() method.

will be called so that the getter in the can get the value of the passcode. provides us method to initialize the methods inside the method. Now we can use use the value of the passCode getter inside our builder.

getter inside our builder. We have created a TextField that will take the value of passCode form the user and when the user will click on the right arrow button then the onPressed property of icon will check the condition if it is true then the user will be navigated to the HomePage.