skip to Main Content

I’m creating a login app and I get an error writing code in many ways, so I’m asking for help.

The code now says to fill in three fields and fill in all fields when you press the login button.

What I want to create is how do I make sure that I can’t log in if I get any of my company code, user number, or password wrong when I log in?

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 {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(35), // appbar and body Space control
          child: Center(
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(10),
                  child: Image.asset(
                    'assets/images/hdis.png',
                    height: 300,
                    width: 300,
                  ),
                ),
                TextFormField(
                  maxLength: 4,
                  controller: DBnameController,
                  decoration: InputDecoration(
                    labelText: 'Company Code',
                  ),
                  validator: (results) {
                    if (results!.isEmpty) {
                      return 'Please enter your company code';
                    }
                    return null;
                  },
                ),
                TextField(
                  maxLength: 6,
                  decoration: InputDecoration(
                    labelText: 'User ID',
                  ),
                ),
                TextField(
                  obscureText: true, //password *** 
                  decoration: InputDecoration(
                    labelText: 'Password',
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 0),
                  child: ElevatedButton(
                    style: OutlinedButton.styleFrom(
                      minimumSize: Size(70, 30),
                    ),
                    onPressed: () async {
                      final String DBname = DBnameController.text;
                      final String empl_no = _empl_noController.text;
                      final String passwd = _passwdController.text;

                      //Verify that each field is empty
                      if (DBname.isNotEmpty || // 
                          empl_no.isNotEmpty ||
                          passwd.isNotEmpty) {
                        print('Please fill in all fields.');
                        return; 
                      }

                      PostgreSQLConnection connection = PostgreSQLConnection(
                        'ip',
                        5432,
                        DBnameController.text,
                        username: 'username',
                        password: 'password',
                      );

                      try {
                        await connection.open();

                        final List<List<dynamic>> results =
                            await connection.query(
                          'SELECT empl_no, passwd passwd FROM spaprsn0 WHERE empl_no = @empl_no AND passwd = @passwd',
                          substitutionValues: {
                            'empl_no': empl_no,
                            'passwd': passwd,
                          },
                        );

                        if (results.isNotEmpty) {
                          final String storedPasswd = results[0][
                              1]; // Assuming the password is in the second column

                          if (passwd == storedPasswd) {
                            print('Success');
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (_) => Menu()),
                            );
                          } else {
                            print('The password is wrong');
                            // 
                          }
                        } else {
                          print('The ID is wrong');
                          // 
                        }
                      } finally {
                        await connection.close();
                      }

                      Navigator.push(
                        
                        context,
                        MaterialPageRoute(builder: (_) => Menu()),
                      );
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(content: Text('Success')),
                      );
                    },
                    child: Text(
                      "Login",
                      style: TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
              ], //children
            ),
          ),
        ),
      ),
    );
  }
}

//Verify that each field is empty
                      if (DBname.isNotEmpty || // 
                          empl_no.isNotEmpty ||
                          passwd.isNotEmpty) {
                        print('Please fill in all fields.');
                        return; 
                      }

Is this the problem?

2

Answers


  1. if you’re looking for something like https://flutter.github.io/assets-for-api-docs/assets/material/text_form_field_error.png
    then you should use TextFormField and wrap all your TextFormFields with Form widget

    for more refer to -> https://docs.flutter.dev/cookbook/forms/validation

    Login or Signup to reply.
  2. Where you verify if each field is empty should look like this:

    //Verify that each field is empty
                          if (DBname.isEmpty || // 
                              empl_no.isEmpty ||
                              passwd.isEmpty) {
                            print('Please fill in all fields.');
                            return; 
                          }
    

    Using isEmpty if any of those are empty it will print the message

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search