0

I am building a flutter program that I want to hook up to my database, I know this is bad practice but it will be for personal use.

The php works and can communicate with the database, but when using an http post request method within my app it does not make it past the first print statement, "Made it to method" and is not calling the php function to update the database. I have the php file hard coded to write to a table and I'm not very concerned with that, I just cannot get the method to run my file.

I have followed a lot of guides online which is why I am using a different local host but still cannot get the method to complete. Any Ideas?

import 'dart:async';
import 'dart:convert';

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

void main() => runApp(new MyApp());

String username='';

class MyApp extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter App with MYSQL',     
      home: new MyHomePage(),
     
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

TextEditingController name=new TextEditingController();
TextEditingController email=new TextEditingController();
TextEditingController mobile=new TextEditingController();

void post() async {
    print("Made it to method");
      final response = await http.post("http://10.0.2.2:8000/config.php", body: {
        "name": name.text,
        "email": email.text,
        "mobile":mobile.text,
      });
      print("completed Method");
    }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Register"),),
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              Text("Username",style: TextStyle(fontSize: 18.0),),
              TextField(   
                controller: name,                
                decoration: InputDecoration(
                  hintText: 'name'
                ),           
                ),
              Text("Email",style: TextStyle(fontSize: 18.0),),
              TextField(  
                controller: email,      
                 decoration: InputDecoration(
                  hintText: 'Email'
                ),                
                ),
                Text("Mobile",style: TextStyle(fontSize: 18.0),),
              TextField(  
                controller: mobile,        
                 decoration: InputDecoration(
                  hintText: 'Mobile'
                ),                
                ),
              
              RaisedButton(
                child: Text("Register"),
                onPressed: (){
                  post();
                },
              ),
            ],
          ),
        ),
      ),
    );
}
}
  • So it does or doesn't print "Made it to method"? Also, you should debug by catching errors and printing the `response` object. – PatrickMahomes Jul 31 '21 at 07:31
  • `I know this is bad practice`... why? Seems a pretty reasonable thing to do. – ADyson Jul 31 '21 at 07:31
  • It makes it to made it to method but not completed method – John Maalouf Jul 31 '21 at 07:38
  • And this is considered bad practice because if hosted publicly anyone with the app would be able to write to the database. A webserver is a more secure solution. – John Maalouf Jul 31 '21 at 07:39
  • Did you have any errors or other useful debug info? Also why are you sending requests over 10/8? Thats not typical a routable IP? – Sherif Jul 31 '21 at 07:49
  • Unfortunately not, I cannot get a debug to print out anything. And the ip/port configuration is because I am running locally on an Android phone and found out from a few online questions that you have to use this ip to access local host – John Maalouf Jul 31 '21 at 07:54
  • Check the accepted answer [here](https://stackoverflow.com/questions/47372568/how-to-point-to-localhost8000-with-the-dart-http-package-in-flutter), maybe it helps you. – Peter Koltai Jul 31 '21 at 09:32

0 Answers0