jojoji54
jojoji54

Reputation: 27

Problems connecting with a DB using Flutter

Im trying to conect my flutter app with a DB in order to get a Login system but I been having some problem with this.

first of all I have two version of Flutter, in the newest one, the code does not works, however in the older one, works prefect.

here is the code that works

class _Login extends State<Login> {
  TextEditingController user = TextEditingController();
  TextEditingController pass = TextEditingController();

Future login() async {
    var url = "https://charlotapp.000webhostapp.com/login.php";
    var response = await http.post(url, body: {
      "username": user.text,
      "password": pass.text,
    });    
 var data = json.decode(response.body);
}

and here is the code that im trying to work

class _Login extends State<Login> {
  TextEditingController user = TextEditingController();
  TextEditingController pass = TextEditingController();

  Future login() async {
    var response = await http.post(
        Uri.http("https://charlotapp.000webhostapp.com/login.php", ""),
        body: {
          "username": user.text,
          "password": pass.text,
        });
    var data = json.decode(response.body);
}

Very similiar isn`t it? however for the second one I get this error;

I/ViewRootImpl@50dab36[MainActivity]( 2374): ViewPostIme pointer 0
I/ViewRootImpl@50dab36[MainActivity]( 2374): ViewPostIme pointer 1
E/flutter ( 2374): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)
E/flutter ( 2374): //charlotapp.000webhostapp.com/login.php
E/flutter ( 2374): ^
E/flutter ( 2374):
E/flutter ( 2374): #0      int._throwFormatException (dart:core-patch/integers_patch.dart:131:5)
E/flutter ( 2374): #1      int._parseRadix (dart:core-patch/integers_patch.dart:157:16)


Any idea? Thanks for your time

Upvotes: 0

Views: 217

Answers (1)

Flak
Flak

Reputation: 2670

When you use Uri.http / https you dont need to parse full url because you are already telling to use http/https.

class _Login extends State<Login> {
  TextEditingController user = TextEditingController();
  TextEditingController pass = TextEditingController();
  Future login() async {
    var response = await http.post(
        Uri.https("charlotapp.000webhostapp.com/login.php", ""),
        body: {
          "username": user.text,
          "password": pass.text,
        });
    var data = json.decode(response.body);
}

refer this: https://flutter.dev/docs/cookbook/networking/send-data#2-sending-data-to-server

Upvotes: 2

Related Questions