Http json.decode()输入意外结束(在字符1处)
在登录用户时,我在Flatter中面临匿名关闭错误。前几天还可以,但现在不行了,我不知道为什么。因此,请提前帮助和感谢。每当我填写详细信息并点击登录按钮时,就会抛出以下错误---Http json.decode()输入意外结束(在字符1处),http,post,flutter,Http,Post,Flutter,在登录用户时,我在Flatter中面临匿名关闭错误。前几天还可以,但现在不行了,我不知道为什么。因此,请提前帮助和感谢。每当我填写详细信息并点击登录按钮时,就会抛出以下错误--- E/flatter(2914):[错误:flatter/shell/common/shell.cc(184)]省道错误:未处理的异常: E/颤振(2914):格式异常:输入意外结束(字符1处) E/颤振(2914): E/颤振(2914):^ E/颤振(2914): E/flatter(2914):#0_Chunked
E/flatter(2914):[错误:flatter/shell/common/shell.cc(184)]省道错误:未处理的异常:
E/颤振(2914):格式异常:输入意外结束(字符1处)
E/颤振(2914):
E/颤振(2914):^
E/颤振(2914):
E/flatter(2914):#0_ChunkedJsonParser.fail(dart:convert/runtime/libconvert_patch.dart:1358:5)
E/flatter(2914):#1_ChunkedJsonParser.close(dart:convert/runtime/libconvert_patch.dart:511:7)
E/flatter(2914):#2 parseJson(dart:convert/runtime/libconvert_patch.dart:30:10)
E/flatter(2914):#3 JsonDecoder.convert(dart:convert/json.dart:540:36)
E/flatter(2914):#4 JsonCodec.decode(dart:convert/json.dart:167:41)
E/Flatter(2914):#5#登录。(套餐:餐厅应用程序/签到省道:81:23)
导入'dart:convert';
进口“包装:颤振/材料.省道”;
导入“套餐:餐厅应用程序/globalVar.dart”;
导入“套餐:餐厅应用程序/主屏幕.dart”;
进口“包装:颤振安全存储/颤振安全存储.dart”;
导入“套餐:餐厅应用程序/注册.dart”;
将“package:http/http.dart”导入为http;
导入“package:flatter_html_view/html_parser.dart”;
类签名扩展StatefulWidget{
@凌驾
_SignInState createState()=>\u SignInState();
}
类_SignInState使用SingleTickerProviderStateMixin扩展状态
{
tab控制器;
TextEditingController_email=新建TextEditingController();
TextEditingController_password=新的TextEditingController();
布尔加载;
最终GlobalKey _scaffoldKey=新GlobalKey
();
@凌驾
void initState(){
//TODO:实现initState
super.initState();
控制器=新的TabController(长度:2,vsync:this);
加载=假;
_电子邮件=新文本编辑控制器(文本:“rajeshvishnani”);
_密码=新文本编辑控制器(文本:Rajesh@MaaKiRasoi");
}
@凌驾
无效处置(){
//TODO:实现dispose
super.dispose();
controller.dispose();
设置状态(){
加载=假;
});
_email.dispose();
_password.dispose();
}
最终的GlobalKey _formKey=GlobalKey();
bool _autoValidate=false;
未来写入程序(字符串用户名、字符串密码、字符串令牌)异步{
最终存储=新存储();
等待存储。写入(键:authTokenKeys,值:token);
打印(等待存储.read(key:authTokenKeys));
等待存储。写入(键:nameKeys,值:username);
打印(等待存储。读取(键:nameKeys));
等待存储。写入(键:passwordKeys,值:password);
}
静态最终字符串authTokenKeys='auth_token';
静态最终字符串nameKeys='username';
静态最终字符串密码键='password';
_异步登录(用户名、密码){
设置状态(){
加载=真;
});
var body=json.encode({
“用户名”:用户名,
“密码”:密码,
});
映射头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
};
等待http
.post(“${GlobalVar.Ip}/wp json/jwt auth/v1/token”,
正文:正文,标题:标题)
.然后((回应){
var body=json.decode(response.body);
如果(response.statusCode==200){
//TODO:您需要存储body['token']以用于某些身份验证
加载=假;
_writer(_email.text、_password.text、body['token']);
Navigator.pushReplacement(上下文,
MaterialPackageRoute(生成器:(BuildContext ctx)=>HomePage());
}否则{
//TODO:警报消息
最终蛇杆=蛇杆(
内容:文本(正文['message']),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
设置状态(){
加载=假;
});
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
钥匙:_scaffoldKey,
resizeToAvoidBottomPadding:false,
主体:容器(
装饰:盒子装饰(
图像:装饰图像(
图像:AssetImage('images/art.png'),
fit:BoxFit.fill,
colorFilter:colorFilter.mode(
颜色。白色12。不透明度(0.2),BlendMode.dStop),
),
),
子:ListView(
收缩膜:对,
物理:弹跳CrollPhysics(),
儿童:[
大小盒子(
高度:MediaQuery.of(context).size.height/30,
),
对齐(
对齐:alignment.topCenter,
孩子:圆环星(
背景颜色:颜色。灰色,
半径:55.0,
背景图片:AssetImage('images/logo.png'),
),
),
大小盒子(
高度:MediaQuery.of(context).size.height/30,
),
堆叠(
对齐:对齐.center,
儿童:[
大小盒子(
高度:MediaQuery.of(context).size.height/300,
孩子:新中心(
子容器:新容器(
身高:10.0,
颜色:颜色。黑色,
),
),
),
划船(
儿童:[
大小盒子(
宽度:MediaQuery.of(context).size.width/4,
),
芯片(
标签:文本(
“登录”,
样式:TextStyle(颜色:Colors.white,fontSize:18.0),
),
背景颜色:颜色(0xFFD1A155),
),
大小盒子(
宽度:MediaQuery.of(context).size.width/35,
子:容器(
宽度:MediaQuery.of(context).size.height/12,
身高:2.0,
颜色:颜色,白色,
),
),
芯片(
标签:文本(
“注册”,
样式:TextStyle(颜色:Colors.white,fontSize:18.0),
),
E/flutter ( 2914): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter ( 2914): FormatException: Unexpected end of input (at character 1)
E/flutter ( 2914):
E/flutter ( 2914): ^
E/flutter ( 2914):
E/flutter ( 2914): #0 _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1358:5)
E/flutter ( 2914): #1 _ChunkedJsonParser.close (dart:convert/runtime/libconvert_patch.dart:511:7)
E/flutter ( 2914): #2 _parseJson (dart:convert/runtime/libconvert_patch.dart:30:10)
E/flutter ( 2914): #3 JsonDecoder.convert (dart:convert/json.dart:540:36)
E/flutter ( 2914): #4 JsonCodec.decode (dart:convert/json.dart:167:41)
E/flutter ( 2914): #5 _SignInState._login.<anonymous closure> (package:restaurant_app/signin.dart:81:23)
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:restaurant_app/globalVar.dart';
import 'package:restaurant_app/homescreen.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:restaurant_app/signup.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_html_view/html_parser.dart';
class SignIn extends StatefulWidget {
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> with SingleTickerProviderStateMixin
{
TabController controller;
TextEditingController _email = new TextEditingController();
TextEditingController _password = new TextEditingController();
bool loading;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>
();
@override
void initState() {
// TODO: implement initState
super.initState();
controller = new TabController(length: 2, vsync: this);
loading = false;
_email = new TextEditingController(text: "rajeshvishnani");
_password = new TextEditingController(text: "Rajesh@MaaKiRasoi");
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
controller.dispose();
setState(() {
loading = false;
});
_email.dispose();
_password.dispose();
}
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
bool _autoValidate = false;
Future _writer(String username, String password, String token) async {
final storage = new FlutterSecureStorage();
await storage.write(key: authTokenKeys, value: token);
print(await storage.read(key: authTokenKeys));
await storage.write(key: nameKeys, value: username);
print(await storage.read(key: nameKeys));
await storage.write(key: passwordKeys, value: password);
}
static final String authTokenKeys = 'auth_token';
static final String nameKeys = 'username';
static final String passwordKeys = 'password';
_login(username, password) async {
setState(() {
loading = true;
});
var body = json.encode({
"username": username,
"password": password,
});
Map<String, String> headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
await http
.post("${GlobalVar.Ip}/wp-json/jwt-auth/v1/token",
body: body, headers: headers)
.then((response) {
var body = json.decode(response.body);
if (response.statusCode == 200) {
// TODO: you need to store body['token'] to use in some authentication
loading = false;
_writer(_email.text, _password.text, body['token']);
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext ctx) => HomePage()));
} else {
// TODO: alert message
final snackBar = SnackBar(
content: Text(body['message']),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
}
setState(() {
loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomPadding: false,
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/art.png'),
fit: BoxFit.fill,
colorFilter: ColorFilter.mode(
Colors.white12.withOpacity(0.2), BlendMode.dstATop),
),
),
child: ListView(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).size.height / 30,
),
Align(
alignment: Alignment.topCenter,
child: CircleAvatar(
backgroundColor: Colors.grey,
radius: 55.0,
backgroundImage: AssetImage('images/logo.png'),
),
),
SizedBox(
height: MediaQuery.of(context).size.height / 30,
),
Stack(
alignment: Alignment.center,
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).size.height / 300,
child: new Center(
child: new Container(
height: 10.0,
color: Colors.black12,
),
),
),
Row(
children: <Widget>[
SizedBox(
width: MediaQuery.of(context).size.width / 4,
),
Chip(
label: Text(
"SIGN IN",
style: TextStyle(color: Colors.white, fontSize: 18.0),
),
backgroundColor: Color(0xFFD1A155),
),
SizedBox(
width: MediaQuery.of(context).size.width / 35,
child: Container(
width: MediaQuery.of(context).size.height / 12,
height: 2.0,
color: Colors.white,
),
),
Chip(
label: Text(
"SIGN UP",
style: TextStyle(color: Colors.white, fontSize: 18.0),
),
backgroundColor: Colors.black87,
),
],
)
],
),
SizedBox(
height: MediaQuery.of(context).size.height / 35,
),
Align(
alignment: Alignment.center,
child: Text(
"Welcome back!",
style: TextStyle(
fontSize: 20.0,
color: Color(0xFFD1A155),
),
)),
SizedBox(
height: MediaQuery.of(context).size.height / 30,
),
Form(
key: _formKey,
autovalidate: _autoValidate,
child: Column(
children: <Widget>[
Theme(
data: ThemeData(
hintColor: Colors.black26,
primaryColor: Color(0xFFD1A155),
),
child: Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15.0),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
//validator: _email.text.isEmpty?:null,
controller: _email,
decoration: InputDecoration(
border:
OutlineInputBorder(borderSide: BorderSide()),
prefixIcon: Icon(
Icons.email,
color: Color(0xFFD1A155),
),
hintText: 'Email Address',
hintStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w400)),
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height / 45,
),
Theme(
data: ThemeData(
primaryColor: Color(0xFFD1A155),
hintColor: Colors.black26),
child: Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15.0),
child: TextFormField(
keyboardType: TextInputType.text,
obscureText: true,
controller: _password,
decoration: InputDecoration(
border:
OutlineInputBorder(borderSide: BorderSide()),
prefixIcon: Icon(
Icons.lock,
color: Color(0xFFD1A155),
),
hintText: 'Password',
hintStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w400)),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 15.0, left: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
ActionChip(
onPressed: () {},
avatar: Checkbox(
value: false,
onChanged: (bool z) {
print(z);
},
activeColor: Color(0xFFD1A155),
),
label: Text("Remember Me"),
backgroundColor: Colors.transparent,
),
],
),
Text(
"Forgot Password?",
style: TextStyle(
color: Color(0xFFD1A155),
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15.0),
child: InkWell(
onTap: () {
_login(_email.text, _password.text);
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => HomePage()));
},
child: loading
? CircularProgressIndicator()
: Container(
height: MediaQuery.of(context).size.height / 13,
//width: MediaQuery.of(context).size.height / 1.8,
decoration: BoxDecoration(
color: Color(0xFFD1A155),
borderRadius: BorderRadius.circular(5.0),
),
child: Center(
child: Text(
"LOGIN",
style: TextStyle(
color: Colors.white, fontSize: 18.0),
),
),
),
),
)
],
),
),
SizedBox(
height: MediaQuery.of(context).size.height / 15,
),
Stack(
alignment: Alignment.center,
children: <Widget>[
SizedBox(
height: 2.0,
child: new Center(
child: new Container(
height: 10.0,
color: Colors.black12,
),
),
),
Container(
height: MediaQuery.of(context).size.height / 18,
width: MediaQuery.of(context).size.height / 11,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(23.0),
color: Colors.white,
border: Border.all(color: Colors.black12)),
child: Center(
child: Text(
"OR",
style: TextStyle(fontSize: 18.0),
)),
),
],
),
SizedBox(
height: MediaQuery.of(context).size.height / 30,
),
Padding(
padding: const EdgeInsets.only(left: 15.0, right: 15.0),
child: Row(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 13,
width: MediaQuery.of(context).size.width / 2.2,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.white,
border: Border.all(color: Colors.black12)),
child: Row(
children: <Widget>[
SizedBox(
width: 18.0,
),
Icon(Icons.tag_faces),
SizedBox(
width: 10.0,
),
Text(
"Facebook",
style: TextStyle(fontSize: 22.0, color: Colors.blue),
),
],
),
),
SizedBox(
width: MediaQuery.of(context).size.width / 40,
),
Container(
height: MediaQuery.of(context).size.height / 13,
width: MediaQuery.of(context).size.width / 2.3,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.white,
border: Border.all(color: Colors.black12)),
child: Row(
children: <Widget>[
SizedBox(
width: 18.0,
),
Icon(Icons.tag_faces),
SizedBox(
width: 10.0,
),
Text(
"Google+",
style: TextStyle(fontSize: 22.0, color: Colors.red),
),
],
),
),
],
),
),
SizedBox(
height: MediaQuery.of(context).size.height / 20,
),
Align(
alignment: Alignment.center,
child: InkWell(
onTap: () => Navigator.push(
context, MaterialPageRoute(builder: (context) => SignUp())),
child: RichText(
text: TextSpan(
text: "Don't have an account?",
style: TextStyle(fontSize: 20.0, color: Colors.black87),
children: <TextSpan>[
TextSpan(
text: ' Sign up',
style: TextStyle(
color: Color(0xFFD1A155),
fontWeight: FontWeight.bold)),
])),
),
),
SizedBox(
height: MediaQuery.of(context).size.height / 30,
),
],
),
),
);
}
}
var body = json.decode(response.body);
if(response.body.isNotEmpty) {
json.decode(response.body);
}