点击登录按钮后出现错误 ->
未处理的异常:类型 '_InternalLinkedHashMap
} 抓住 (e) { 扔e; // 这里 } 请帮助我我不明白如何解决
未处理的异常:类型 '_InternalLinkedHashMap
这是我的 auth.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:login_and_signup/Utils/api.dart';
class Auth with ChangeNotifier {
var MainUrl = Api.authUrl;
Future<void> Authentication(String user_name, String user_email,
String mobile_no, String password, String action) async {
try {
var url = Uri.parse('$MainUrl&action=$action');
print(url);
if (action == 'registration') {
final responce = await http.post(url, headers: {
"Accept": "Application/json"
}, body: {
"user_name": user_name,
'user_email': user_email,
"mobile_no": mobile_no,
"password": password,
});
print("Response" + responce.body);
final responceData = json.decode(responce.body);
print("responceData" + responceData);
} else {
final responce = await http.post(url, headers: {
"Accept": "Application/json"
}, body: {
"user_name": user_name,
'user_email': user_email,
"username": mobile_no,
"password": password,
});
print("Response" + responce.body);
final responceData = json.decode(responce.body);
print("responceData" + responceData);
}
notifyListeners();
} catch (e) {
throw e;
}
}
Future<void> login(
String user_name, String user_email, String mobile_no, String password) {
return Authentication(user_name, user_email, mobile_no, password, 'login');
}
Future<void> signUp(
String user_name, String user_email, String mobile_no, String password) {
return Authentication(
user_name, user_email, mobile_no, password, 'registration');
}
}
这是我的 login.dart 页面
// import 'package:flutter/material.dart';
// import 'package:login_signup_with_api/Providers/auth.dart';
// import 'package:login_signup_with_api/Screens/signup.dart';
// import 'package:login_signup_with_api/Utils/http_exception.dart';
import 'package:flutter/material.dart';
import 'package:login_and_signup/Providers/auth.dart';
import 'package:login_and_signup/Screens/home_screen.dart';
// import 'package:login_and_signup/Providers/login_auth.dart';
import 'package:login_and_signup/Screens/signup.dart';
import 'package:login_and_signup/Utils/http_exception.dart';
import 'package:provider/provider.dart';
class LoginScreen extends StatefulWidget {
static const String routeName = "/login";
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final GlobalKey<FormState> _formKey = GlobalKey();
TextEditingController _mobileController = new TextEditingController();
TextEditingController _passwordController = new TextEditingController();
Map<String, String> _authData = {
'user_name': '',
'user_email': '',
'username': '',
'password': ''
};
Future _submit() async {
print('aa' + _authData['username']!);
if (!_formKey.currentState!.validate()) {
return;
}
_formKey.currentState!.save();
try {
await Provider.of<Auth>(context, listen: false)
.login(_authData['user_name']!, _authData['user_email']!,
_authData['username']!, _authData['password']!)
.then((_) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => HomeScreen()));
});
print('aaaa' + _authData['username']!);
} catch (error) {
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
// backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Container(
child: Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.65,
width: MediaQuery.of(context).size.width * 0.85,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(360),
bottomRight: Radius.circular(360)),
color: Colors.blue),
),
Container(
padding:
EdgeInsets.only(top: 50, left: 20, right: 20, bottom: 50),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Sign In",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 40),
),
SizedBox(
height: 10,
),
Text(
"Sign in with your username or email",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 10),
),
Form(
key: _formKey,
child: Container(
padding: EdgeInsets.only(top: 50, left: 20, right: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"username",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 12),
),
TextFormField(
// obscureText: true,
controller: _mobileController,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
prefixIcon: Icon(
Icons.vpn_key,
color: Colors.white,
)),
validator: (value) {
if (value!.isEmpty || value.length < 1) {
return 'valid no';
}
},
onSaved: (value) {
_authData['username'] = value!;
},
),
SizedBox(
height: 10,
),
SizedBox(
height: 10,
),
Text(
"Password",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 12),
),
TextFormField(
controller: _passwordController,
obscureText: true,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
prefixIcon: Icon(
Icons.vpn_key,
color: Colors.white,
)),
validator: (value) {
if (value!.isEmpty || value.length < 5) {
return 'Password is to Short';
}
},
onSaved: (value) {
_authData['password'] = value!;
},
),
Container(
padding: EdgeInsets.only(top: 40),
width: 140,
child: RaisedButton(
onPressed: () {
print(_authData['username']);
print(_authData['password']);
_submit();
},
shape: RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(10.0),
),
child: Text(
'Sign In',
style: TextStyle(color: Colors.white),
),
color: Colors.green),
),
Align(
alignment: Alignment.bottomRight,
child: InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (ctx) => SignUpScreen()));
},
child: Container(
padding: EdgeInsets.only(top: 90),
child: Text(
"Create Account",
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.blue,
fontSize: 16),
),
),
),
)
],
),
),
),
],
),
),
],
),
),
),
);
}
void _showerrorDialog(String message) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text(
'An Error Occurs',
style: TextStyle(color: Colors.blue),
),
content: Text(message),
actions: <Widget>[
FlatButton(
child: Text('Okay'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
);
}
}
答案 0 :(得分:1)
这是您收到此错误的地方:
响应数据的类型是 _InternalLinkedHashMap<String, dynamic>
修复不正确的连接。
print("responceData" + responceData);
// Replace this with
print("responceData" + responceData.toString());
答案 1 :(得分:1)
您需要对帖子请求正文进行编码。
Map<String,dynamic> body={
"user_name": user_name,
'user_email': user_email,
"mobile_no": mobile_no,
"password": password,
};
final responce = await http.post(url, headers: {
"Accept": "Application/json"
}, body: json.encode(body));
并更改打印行。
print("responceData" + responceData.toString());
答案 2 :(得分:1)
您应该使用 jsonEncode(requestBody) 对您的请求正文进行编码。 也在打印响应时使用 $ 和字符串。
print("响应:${responce.body}");