gpt4 book ai didi

flutter - 未处理的异常 : type '_InternalLinkedHashMap' is not a subtype of type 'String

转载 作者:行者123 更新时间:2023-12-05 09:32:18 33 4
gpt4 key购买 nike

点击登录按钮后出现错误->未处理的异常:类型“_InternalLinkedHashMap ”不是类型“String

”的子类型

} 捕获(e){扔 e;//这里}请帮助我,我不明白如何解决

未处理的异常:类型“_InternalLinkedHashMap ”不是类型“String”的子类型

这是我的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();
},
)
],
),
);
}
}

最佳答案

您应该使用 jsonEncode(requestBody) 对您的请求主体进行编码。同样在打印响应时使用 $ 和 String。

print("响应:${responce.body}");

关于flutter - 未处理的异常 : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68204723/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com