skip to Main Content

I’m working on an Login app

On the login screen, there are three TextFields: user number, password, and company code(= database name)
When login, enter userID, password, and company code(= database name) and click the login button
I want to check the ID/password by accessing the DB of the information entered in the database name.

import 'package:flutter/material.dart';
import 'package:hdislogin/screen/menu.dart';
import 'package:postgres/postgres.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginPage(),
    );
  }
}

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController DBnameController = TextEditingController();
  final TextEditingController _empl_noController = TextEditingController();
  final TextEditingController _passwdController = TextEditingController();

  Future<void> _login() async {
    //final String DBname = DBnameController.text;
    final String empl_no = _empl_noController.text;
    final String passwd = _passwdController.text;

    PostgreSQLConnection connection = PostgreSQLConnection(
      'ip',
      5432,
      'DBName',
      username: 'username',
      password: 'password',
    );

    try {
      await connection.open();

      final List<List<dynamic>> results = await connection.query(
        'SELECT * from table',
        substitutionValues: {
          'empl_no': empl_no,
          'passwd': passwd,
        },
      );

      if (results.isNotEmpty) {
        print('success');
        Navigator.push(
          context,
          MaterialPageRoute(builder: (_) => Menu()),
        );
      } else {
        print('fail');
      }
    } finally {
      await connection.close();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(35), // 
          child: Center(
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: Image.asset(
                    'assets/images/hdis.png',
                    height: 300,
                    width: 300,
                  ),
                ),
                 TextField(
                  maxLength: 4,
                  //controller: DBnameController = 'DBname',
                  decoration: InputDecoration(
                    labelText: 'CompanyCode',
                  ),
                ),
                 TextField(
                  maxLength: 6,
                  decoration: InputDecoration(
                    labelText: 'userID',
                  ),
                ),
                 TextField(
                  obscureText: true, //password *** 
                  decoration: InputDecoration(
                    labelText: 'password',
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 0),
                  child: ElevatedButton(
                    style: OutlinedButton.styleFrom(
                      minimumSize: Size(70, 30),
                    ),
                    onPressed: () {
                      
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (_) => Menu()),
                      );
                    },
                    child: Text(
                      "Login",
                      style: TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
              ], //children
            ),
          ),
        ),
      ),
    );
  }
}

Currently, the code is ID password is checkable and only the database name cannot be checked.

How do I add the ability to receive and check database names in TextField?

What I’ve tried to do is….

class _LoginPageState extends State<LoginPage>
  //added part
  final TextEditingController DBnameController = TextEditingController(); 

.
.
.

 TextField(
                  maxLength: 4,
     //added part //controller: DBnameController = 'DBname', 
                  decoration: InputDecoration(
                    labelText: 'CompanyCode',
                  ),
                ),

2

Answers


  1. Are you able to update the example you have provided. From what i can see, you have commented out the DBName textfield controller, and have added the field directly into PostgreSQLConnection

    Try to change it to this:

    Future<void> _login() async {
    final String DBname = DBnameController.text;
    final String empl_no = _empl_noController.text;
    final String passwd = _passwdController.text;
    
    PostgreSQLConnection connection = PostgreSQLConnection(
      'ip',
      5432,
     DBName,
      username: 'username',
      password: 'password',
    );
    
    try {
      await connection.open();
    
      final List<List<dynamic>> results = await connection.query(
        'SELECT * from table',
        substitutionValues: {
          'empl_no': empl_no,
          'passwd': passwd,
        },
      );
    
      if (results.isNotEmpty) {
        print('success');
        Navigator.push(
          context,
          MaterialPageRoute(builder: (_) => Menu()),
        );
      } else {
        print('fail');
      }
    } finally {
      await connection.close();
    }
    

    }

    Login or Signup to reply.
  2. Do the following changes

    PostgreSQLConnection connection = PostgreSQLConnection(
      'ip',
      5432,
      DBnameController.text,
      username: 'username',
      password: 'password',
    );
    
    
    
    
    TextField(
                      maxLength: 4,
    controller: DBnameController, 
                      decoration: InputDecoration(
                        labelText: 'CompanyCode',
                      ),
                    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search