0

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

1
  • Try changing from Uri.http to Uri.https Commented Mar 23, 2021 at 18:27

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Finally I solved the problem changing Uri.https with Uri.parse, however, your solution works too, thanks for yor help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.