X
    Categories: Flutter

GetX Validations | Flutter validations using GetX library

 

GetX Validation :

GetX Validations is based on the Flutter GetX library using which we can easily validate the form fields much easier than before. There are different options available to validate fields.

For example when you consider a user registration form there we will be having name, phone number, age, email address and other fields.

All the above mentioned fields can be easily validated using GetUtils which is a part of GetX library using a single line statement.

You can specify the condition to validate and as a parameter pass the value that needs to be validated and later on specify the true and false cases to be executed.

GetX Validations specify the key role in the process of submitting data so as to avoid unwanted data to be passed through the api thereafter avoid the unwanted resource usage.

 

GetX Validation Video Tutorial :

You can go through the below video tutorial for more information on GetX validations implementation.

 

pubspec.yaml :

You need to add GetX dependency to make use of GetX Validations and provide the latest version to avoid the code level issues.

dependencies:
  flutter:
    sdk: flutter
  get: 4.1.4

 

Validations.dart :

Providing the source code for validations of the fields name, password and email. We have provided three controllers using which we fetch the data and process.

We have performed validations using three different styles and you can opt for other ways by going through the video tutorial provided above.

We have added a controller using which we can fetch the data

final nameController = TextEditingController();

 

Add a Text Form Field using which we accept user input field.

TextFormField(
  controller: nameController,
  decoration: InputDecoration(hintText: ' name...'),
),

 

Add a validation for name using is Length parameter and provide the success and fail conditions accordingly.

GetUtils.isLengthGreaterThan(nameController.text, 6) ? print('Name is valid') : print('Name is invalid!!!');

 

GetX Validations Full Code :

Providing the full code for getx flutter validations.

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class Validations extends StatefulWidget {
  const Validations({Key? key}) : super(key: key);

  @override
  _ValidationsState createState() => _ValidationsState();
}

class _ValidationsState extends State<Validations> {
  @override
  Widget build(BuildContext context) {
    final nameController = TextEditingController();
    final pwdController = TextEditingController();
    final emailController = TextEditingController();

    return Scaffold(
      appBar: AppBar(
        title: Text('Validations'),
      ),
      body: Column(
        children: [
          TextFormField(
            controller: nameController,
            decoration: InputDecoration(hintText: ' name...'),
          ),
          TextFormField(
            controller: pwdController,
            decoration: InputDecoration(hintText: ' password...'),
            obscureText: true,
          ),
          TextFormField(
            controller: emailController,
            decoration: InputDecoration(hintText: ' email...'),
          ),
          TextButton(onPressed: () {
            setState(() {
              GetUtils.isLengthGreaterThan(nameController.text, 6) ? print('Name is valid') : print('Name is invalid!!!');
              GetUtils.isMD5('3bc8471bb664e23c0ce91ea1872fcb85') ? print('Access Granted') : print('Access Denied');
              GetUtils.isEmail(emailController.text) ? print('Email is valid') : print('Email is invalid!!!');

            });
          },
              child: Text("Validate"))
        ],
      ),
    );
  }
}

 

GetX Validations Output :

The screen below depicts the usage of GetX Validations implementation in flutter app.

 

If you are having any query’s in this tutorial on Flutter GetX validations do let us know in the comment section below.If you like this tutorial do like and share is for more interesting tutorials.

 

abhishek:
Related Post

This website uses cookies.