Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] php – Unhandled Exception: FormatException: Unexpected character (at character 1) E/flutter ( 6084):
E/flutter ( 6084): ^

Posted on November 11, 2022 By

Solution 1 :

Your code looks really error prone, but focusing on your issue: The exception looks quite clear, your response parsing is breaking.
(I would also say that you should wrap your code with a try/catch to prevent any code breaking and catch properly issues).

Let’s fix your issue:

var data = jsonDecode(response.body);

This converts your string (response.body) into a json, which means data is either Map<String, dynamic> or List<dynamic> (Where dynamic is a Map or another nested List), so the following makes no sense

if (data == "success") {

Now, let’s have a look at your php code:

echo json_encode("success");

I’m not a php expert, but from the documentation looks like it should be used like the following:

$response = array("result" => "Success");
echo json_encode($response);

Let’s go back to your dart code now:

    var success = false;
    try{

      final baseUrl = "http://192.168.43.150"; // Use final wherever you can
      final url = "$baseUrl/v1_flutter/lib/php/connection.php"; // Improving flexibility

      final body = <String, String>{
        "email": email.text,
        "pass": password.text,
      }; // Decouple in variables for readability

      final headers = <String, String>{"Accept":"application/json"}; // Fix typo

      final response = await http.post(Uri.parse(url), body: body, headers: headers);
      final data = jsonDecode(response.body);
      success = data["result"] == "Success";
   } catch(e) {
      print("Catched an error!");
      print(e);
      success = false;
   }

   if(success) {
   ...

Problem :

php file:

 $email=$_POST['email'];

    $passworda=$_POST['passworda'];

$sql="SELECT * FROM user WHERE email='".$email."'AND passworda='".$password."' ";
$result=mysqli_query($db,$sql);
$count=mysqli_num_rows($result);
if($count>=1){
echo json_encode("success");
}
else
{
echo json_encode("error");
}

logine page Flutter:

class Login extends StatelessWidget {
  TextEditingController email = TextEditingController();
  TextEditingController password = TextEditingController();

  Future login(BuildContext cont) async {
    if (email.text == "" || password.text == "") {
      Fluttertoast.showToast(
        msg: "please complete!",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        fontSize: 16.0,
      );
    } else {
      var url = "http://192.168.43.150/v1_flutter/lib/php/connection.php";
      var response = await http.post(Uri.parse(url), body: {
        "email": email.text,
        "pass": password.text,
      }, headers: {"Accept":"applicarion/json"});
      var data = jsonDecode(response.body);

      if (data == "success") {
        Navigator.pop(cont);
        Navigator.pushNamed(cont, "/registre");
      } else {
        Fluttertoast.showToast(
          msg: "The user and password does not exist!",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          fontSize: 16.0,
        );
      }
    }}

Console:

E/flutter ( 6084): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)]
Unhandled Exception: FormatException: Unexpected character (at
character 1)
E/flutter ( 6084):
E/flutter ( 6084): ^
E/flutter ( 6084):
E/flutter ( 6084): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
E/flutter ( 6084): #1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9)
E/flutter ( 6084): #2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22)
E/flutter ( 6084): #3 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
E/flutter ( 6084): #4 JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 6084): #5 JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter ( 6084): #6 jsonDecode (dart:convert/json.dart:155:10)
E/flutter ( 6084): #7 Login.login (package:mes_v1/pages/Authentification/login.dart:25:18)
E/flutter ( 6084):
E/flutter ( 6084):

Comments

Comment posted by Matteo NNZ

I don’t know PHP but your script seems very vulnerable to SQL injections

READ  [FIXED] android - setState doesn't change decoration of TextField
Powered by Inline Related Posts

Comment posted by Dawood ibn Kareem

What language is the second snippet? It’s not Java.

Comment posted by Matteo NNZ

@DawoodibnKareem it’s Dart, the language used in the Flutter SDK (made to build UI cross-platform). Clearly not Java, indeed.

Comment posted by Dawood ibn Kareem

OK, I’ve replaced the Java tag with a Dart one.

Android Tags:android, dart, flutter, json, php

Post navigation

Previous Post: [FIXED] java – is permission needed when choosing image from gallery on android?
Next Post: [FIXED] Android Java: How to get component in a custom view? (Trying to access components via findByViewId and get null)

Related Posts

[FIXED] java – On Insert Data in Firebase RealTime Database it deletes previous data Android
[FIXED] navbar – Change text color in navigation bar Android Studio with getSupportActionBar() Android
[FIXED] android intent – Kotlin – start activity after getting an alert and click OK Android
[FIXED] java – Android : Start a transition after another one ends Android
[FIXED] android – Can I make a custom constructor for a custom view? Android
[FIXED] android – how to fetch only the new documents from firebase? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme