0

Im using Flutter and I´m trying to conect my aplicaction to a database, however I can conect because always appear that error in this sentence (in url):

var response = await http.post(url, body: json.encode(data));

, Does anyone know what the problem is? Here is the code:

class LoginUserState extends State {

  // For CircularProgressIndicator.
  bool visible = false ;

  // Getting value from TextField widget.
  final emailController = TextEditingController();
  final passwordController = TextEditingController();

Future userLogin() async{

  // Showing CircularProgressIndicator.
  setState(() {
  visible = true ; 
  });

  // Getting value from Controller
  String email = emailController.text;
  String password = passwordController.text;

  // SERVER LOGIN API URL
  var url = 'https://fluer-examples.com/login_user.php';

  // Store all data with Param Name.
  var data = {'email': email, 'password' : password};

  // Starting Web API Call.
  var response = await http.post(url, body: json.encode(data));

  // Getting Server response into variable.
  var message = jsonDecode(response.body);

  // If the Response Message is Matched.
  if(message == 'Login Matched')
  {

    // Hiding the CircularProgressIndicator.
      setState(() {
      visible = false; 
      });

    // Navigate to Profile Screen & Sending Email to Next Screen.
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => ProfileScreen(email : emailController.text))
      );
  }else{

    // If Email or Password did not Matched.
    // Hiding the CircularProgressIndicator.
    setState(() {
      visible = false; 
      });

    // Showing Alert Dialog with Response JSON Message.
    showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: new Text(message),
        actions: <Widget>[
          FlatButton(
            child: new Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
    );}

}
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
jojoji54
  • 27
  • 1
  • 7

1 Answers1

1

Always good to read the documentation when you have a problem like this. In the http post method signature:

Future<Response> post (
Uri url,
{Map<String, String>? headers,
Object? body,
Encoding? encoding}
)

the first parameter is a type of Uri, not a String. Here is the documentation about Uri

You will want to use Url.http method to create the URI and pass in your url:

Uri.http(
String authority,
String unencodedPath,
[Map<String, dynamic>? queryParameters]
)

Example - Uri.http("https://fluer-examples.com/login_user.php", "");

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • That works! thanksss – jojoji54 Mar 12 '21 at 23:52
  • That's not how `Uri.http` is meant to be used. If it happens to work, I would not rely on it to continue working. Really you should use [`Uri.parse`](https://api.dart.dev/stable/dart-core/Uri/parse.html) instead. – jamesdlin Mar 13 '21 at 00:03
  • See https://stackoverflow.com/q/66619895/ for an example of how using `Uri.http`/`Uri.https` is wrong. – jamesdlin Mar 14 '21 at 00:48