0
votes

I am new to flutter and I am trying to connect my app to the MySQL database. Which is located in localhost(xampp). I am using mysql1 for this purpose. However, when I am trying to connect to the database I am encountering this error.

"E/flutter (16289): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: SocketException: Socket has been closed"

I have checked a lot of solutions but nothing seems to work. The essential code is posted below.

insertDatabase(product productlist) async{//ignore productlist

   ConnectionSettings settings = new ConnectionSettings(//trying to connect to local host
       host: '10.0.2.2',//i found from web that avd need this address to connect to local host
       port: 3306,
       user: '',
       password:'',
       db: 'databsename'
   );
   dynamic db =  await MySqlConnection.connect(settings);//trying to connect to database.



   try{
     var result = await db.query('insert into users (productBarcode, productName, productPrice,productQuantity) values (?, ?, ?)', [productBarcode.text,productName.text,productPrice.text,productQuantity.text]);
   return(result);
   }
    catch(e){
     print("Unable to upload data."+e.toString());
   }

 } 
2

2 Answers

0
votes

Well it was issue with port and typing right port fixed the issue.

0
votes

I was with this error and what i did is to change localhost for 10.0.2.2, and it worked fine!