Flutter Unhandled Exception: Connection Time Out

19 views Asked by At

I've got this error on my flutter client ( registration page ) It is a registration page and the backend is on php. But the Connection timed out error is showing. I've done lot of things from the internet but nothings works well. I'm using linux(Ubuntu). For database I've used mysql

E/flutter (21877): #0      IOClient.send (package:http/src/io_client.dart:94:7)
E/flutter (21877): <asynchronous suspension>
E/flutter (21877): #1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:32)
E/flutter (21877): <asynchronous suspension>
E/flutter (21877): #2      _withClient (package:http/http.dart:166:12)
E/flutter (21877): <asynchronous suspension>
E/flutter (21877): #3      _RegistrationState._registerUser (package:thefinalproject/homepage/registration.dart:21:18)
E/flutter (21877): <asynchronous suspension>
E/flutter (21877): 

type here

My flutter code:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:fluttertoast/fluttertoast.dart';

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

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

class _RegistrationState extends State<Registration> {
  TextEditingController _fnameController = TextEditingController();
  TextEditingController _lnameController = TextEditingController();
  TextEditingController _phoneController = TextEditingController();
  //bool _registrationComplete = false;

Future _registerUser() async{
  var url = Uri.parse("http://192.168.152.193:3000/register.php");
  var response = await http.post(url,
      body: {
    "firstname" : _fnameController.text,
    "lastname" : _lnameController.text,
    "phonenumber" : _phoneController.text,

             },
  );
  var data =json.decode(response.body);
  if(data == "Error") {
    Fluttertoast.showToast(
        msg: "Already Exists",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }else {
    Fluttertoast.showToast(
        msg: "Successfull",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.green,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BillSpark'),
      ),
      body: Container(
        padding: const EdgeInsets.symmetric(vertical: 25.0, horizontal: 25.0),
        child: Column(
          children: <Widget>[
            //firstname
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: TextFormField(
                controller: _fnameController,
                decoration: InputDecoration(
                    labelText: "First Name",
                    hintText: "Enter your firstname",
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(15.0))),
              ),
            ),

            //lastname
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: TextFormField(
                controller: _lnameController,
                keyboardType: TextInputType.name,
                decoration: InputDecoration(
                    labelText: "Last Name",
                    hintText: "lastname",
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(15.0))),
              ),
            ),

            //phone number
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: TextFormField(
                controller: _phoneController,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                    labelText: "Enter you phone number",
                    hintText: "enter phone number",
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(15.0))),
              ),
            ),

            //Register Button
            // Register Button
            Padding(
              padding: const EdgeInsets.all(10.0),
              child: Container(
                height: 100,
                color: Colors.white,
                width: 120,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        foregroundColor: Colors.white,
                        backgroundColor: Colors.green,
                        padding: const EdgeInsets.all(20.0),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                      ),
                      onPressed: () {
                        // Call the _registerUser() method to initiate registration
                        _registerUser();
                      },
                      child: const Text('Register'),
                    ),
                  ],
                ),
              ),
            ),

            // Do not have an account?
            const Center(
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: Center(
                  child: Text(
                    'Do not have an account?',
                    style: TextStyle(fontSize: 18),
                  ),
                ),
              ),
            ),

            // Show registration success message if registration is complete
            /*if (_registrationComplete)
              const Text(
                'Registration Successful!',
                style: TextStyle(fontSize: 18, color: Colors.green),
              ),*/
          ],
        ),
      ),
    );
  }
}


**And my backend code is here: **

<?php
$db = mysqli_connect('localhost', 'root', '', 'billspark');
if (!$db) {
    echo "Database connection failed";
}

$fname = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$lname = isset($_POST['lastname']) ? $_POST['lastname'] : '';
$pnumber = isset($_POST['phonenumber']) ? $_POST['phonenumber'] : '';

$sql = "SELECT * FROM Registration WHERE phonenumber = '".$pnumber."'";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if ($count == 1) {
    echo json_encode("Error");
} else {
    $insert = "INSERT INTO Registration (firstname, lastname, phonenumber) VALUES ('".$fname."', '".$lname."', '".$pnumber."')";
    $query = mysqli_query($db, $insert);
    if ($query) {
        echo json_encode("Success");
    }
}
?>


Where is the issue?`

I want to solve this issue and want to run this code without any error.

0

There are 0 answers