提问人:Jizino 提问时间:11/9/2022 最后编辑:ADysonJizino 更新时间:11/9/2022 访问量:622
使用 PHP api 的 Flutter Mysql
Flutter Mysql using PHP api
问:
这个错误我有很长一段时间的问题,我真的不知道如何解决它。我从各种来源看到JSON解码应该有问题,但是我已经按照说明做了几次,那里没有出现问题。错误如下:
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 1)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www....
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#4 JsonDecoder.convert (dart:convert/json.dart:612:36)
#5 JsonCodec.decode (dart:convert/json.dart:216:41)
#6 _RegisterState.register (package:projectbicycle/example/register.dart:25:21)
<asynchronous suspension>
注册.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart' as http;
import 'package:projectbicycle/dashboard.dart';
import 'package:projectbicycle/main.dart';
class Register extends StatefulWidget {
const Register({Key? key}) : super(key: key);
@override
_RegisterState createState() => _RegisterState();
}
class _RegisterState extends State<Register> {
TextEditingController user = TextEditingController();
TextEditingController pass = TextEditingController();
Future register() async {
var url = Uri.http("95.105.178.9", '/api_bike/register.php', {'q': '{http}'});
var response = await http.post(url, body: {
"username": user.text.toString(),
"password": pass.text.toString()
});
var data = json.decode(response.body);
if (data == "Error") {
Fluttertoast.showToast(
backgroundColor: Colors.orange,
textColor: Colors.white,
msg: 'User already exit!',
toastLength: Toast.LENGTH_SHORT,
);
} else {
Fluttertoast.showToast(
backgroundColor: Colors.green,
textColor: Colors.white,
msg: 'Registration Successful',
toastLength: Toast.LENGTH_SHORT,
);
Navigator.push(context,
MaterialPageRoute(
builder: (context) => DashBoard(),
),
);
}
}
bool isPasswordVisible = false;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
height: double.infinity,
width: double.infinity,
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.teal.shade200, Colors.purple.shade900])),
child: SingleChildScrollView(
child: Column(
children: [
Align(
alignment: Alignment.topRight,
child: Container(
height: 100,
width: 300,
decoration: const BoxDecoration(
gradient:
LinearGradient(colors: [Colors.red, Colors.yellow]),
boxShadow: [
BoxShadow(
blurRadius: 4,
spreadRadius: 3,
color: Colors.black12)
],
borderRadius: BorderRadius.only(
topLeft: Radius.circular(200),
bottomRight: Radius.circular(200))),
child: Padding(
padding: const EdgeInsets.only(bottom: 35, left: 65),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
const Text(
'Let\'s',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white,
shadows: [
Shadow(
color: Colors.black45,
offset: Offset(1, 1),
blurRadius: 5)
]),
),
Text(
' Register',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.pink.shade600,
shadows: const [
Shadow(
color: Colors.black45,
offset: Offset(1, 1),
blurRadius: 5)
]),
),
],
),
),
),
),
const SizedBox(
height: 40,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30)
.copyWith(bottom: 10),
child: TextField(
controller: user,
style: const TextStyle(color: Colors.white, fontSize: 14.5),
decoration: InputDecoration(
prefixIconConstraints:
const BoxConstraints(minWidth: 45),
prefixIcon: const Icon(
Icons.alternate_email_outlined,
color: Colors.white70,
size: 22,
),
border: InputBorder.none,
hintText: 'Enter Username',
hintStyle: const TextStyle(
color: Colors.white60, fontSize: 14.5),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(100).copyWith(
bottomRight: const Radius.circular(0)),
borderSide:
const BorderSide(color: Colors.white38)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(100).copyWith(
bottomRight: const Radius.circular(0)),
borderSide:
const BorderSide(color: Colors.white70))),
),
),
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30)
.copyWith(bottom: 10),
child: TextField(
controller: pass,
style: const TextStyle(color: Colors.white, fontSize: 14.5),
obscureText: isPasswordVisible ? false : true,
decoration: InputDecoration(
prefixIconConstraints:
const BoxConstraints(minWidth: 45),
prefixIcon: const Icon(
Icons.lock,
color: Colors.white70,
size: 22,
),
suffixIconConstraints:
const BoxConstraints(minWidth: 45, maxWidth: 46),
suffixIcon: GestureDetector(
onTap: () {
setState(() {
isPasswordVisible = !isPasswordVisible;
});
},
child: Icon(
isPasswordVisible
? Icons.visibility
: Icons.visibility_off,
color: Colors.white70,
size: 22,
),
),
border: InputBorder.none,
hintText: 'Enter Password',
hintStyle: const TextStyle(
color: Colors.white60, fontSize: 14.5),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(100).copyWith(
bottomRight: const Radius.circular(0)),
borderSide:
const BorderSide(color: Colors.white38)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(100).copyWith(
bottomRight: const Radius.circular(0)),
borderSide:
const BorderSide(color: Colors.white70))),
),
),
const SizedBox(
height: 50,
),
GestureDetector(
onTap: () {
register();
},
child: Container(
height: 53,
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 30),
alignment: Alignment.center,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
blurRadius: 4,
color: Colors.black12.withOpacity(.2),
offset: const Offset(2, 2))
],
borderRadius: BorderRadius.circular(100)
.copyWith(bottomRight: const Radius.circular(0)),
gradient: LinearGradient(colors: [
Colors.purple.shade600,
Colors.amber.shade900
])),
child: Text('Signup',
style: TextStyle(
color: Colors.white.withOpacity(.8),
fontSize: 15,
fontWeight: FontWeight.bold)),
),
),
const SizedBox(
height: 50,
),
const Text('Already have an account?',
style: TextStyle(color: Colors.white70, fontSize: 13)),
const SizedBox(
height: 20,
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const MyHomePage()),
);
},
child: Container(
height: 53,
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 30),
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border.all(color: Colors.white60),
borderRadius: BorderRadius.circular(100)
.copyWith(bottomRight: const Radius.circular(0)),
),
child: Text('Login',
style: TextStyle(
color: Colors.white.withOpacity(.8),
fontSize: 15,
fontWeight: FontWeight.bold)),
),
),
const SizedBox(
height: 20,
)
],
),
),
),
),
);
}
}
register.php
<?php
$db = mysqli_connect('localhost','root','','userdata');
if(!$db)
{
echo "Database connection failed";
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT username FROM users WHERE username = '".$username."'";
$result = mysqli_query($db,$sql);
$count = mysqli_num_rows($result);
if($count == 1){
echo json_encode("Error");
}else{
$insert = "INSERT INTO users(username,password) VALUES ('".$username."','".$password."')";
$query = mysqli_query($db,$insert);
if($query){
echo json_encode("Success");
}
}
?>
我将非常感谢任何帮助,正如我所说,我知道解码显然应该有问题,但我真的不知道在哪里以及如何解决它
我尝试了不同的解码变体和几个直接说明,提到的一个是它工作的 YouTube 教程。并且它没有显示任何错误。
答: 暂无答案
评论
root