Json 类型';字符串';不是类型为';int';属于';指数';错误
我在尝试将用户登录到应用程序时遇到了这个问题 登录详细信息将被发送到服务器并在之后发送 必要的身份验证将结果发送回用户(仅将用户名和生成的令牌发送回用户-仅需要此数据), 但是,在运行forEach插入数据时,它会抛出一个错误类型“String”不是“index”的“int”类型的子类型,无法精确定位导致此问题的确切行 如果有人能帮我解决这个问题 Login.dartJson 类型';字符串';不是类型为';int';属于';指数';错误,json,flutter,dart,Json,Flutter,Dart,我在尝试将用户登录到应用程序时遇到了这个问题 登录详细信息将被发送到服务器并在之后发送 必要的身份验证将结果发送回用户(仅将用户名和生成的令牌发送回用户-仅需要此数据), 但是,在运行forEach插入数据时,它会抛出一个错误类型“String”不是“index”的“int”类型的子类型,无法精确定位导致此问题的确切行 如果有人能帮我解决这个问题 Login.dart class Login with ChangeNotifier{ User user; String token;
class Login with ChangeNotifier{
User user;
String token;
Login({this.user,this.token});
Login.fromJson(Map<String, dynamic> json) {
user = json['user'] != null ? new User.fromJson(json['user']) : null;
token = json['token'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.user != null) {
data['user'] = this.user.toJson();
data['token'] = this.token;
}
return data;
}
}
class User with ChangeNotifier{
String createdAt;
String sId;
String userName;
String password;
int iV;
User(
{this.createdAt,
this.sId,
this.userName,
this.password,
this.iV,
});
User.fromJson(Map<String, dynamic> json) {
createdAt = json['createdAt'];
sId = json['_id'];
userName = json['username'];
password = json['password'];
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['createdAt'] = this.createdAt;
data['_id'] = this.sId;
data['username'] = this.userName;
data['password'] = this.password;
data['__v'] = this.iV;
return data;
}
}
飞镖
class LoginHome extends StatefulWidget {
@override
_LoginHomeState createState() => _LoginHomeState();
}
class _LoginHomeState extends State<LoginHome> {
final _form = GlobalKey<FormState>();
var _dLogin = User(
createdAt: DateTime.now().toString(),
userName: '',
password: '',
Future<void> _savedetails() async{
final isValid = _form.currentState.validate();
if(!isValid) {
return;
}
);
_form.currentState.save();
await Provider.of<LoginProvider>(context, listen: false).addLoginDetails(_dLogin)
.catchError((error) {
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Network error...'),));
}
);
@override
Widget build(BuildContext context) {
return
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
Container(
child: SingleChildScrollView(
child: Form(
key:_form,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 70,
width: double.infinity,
margin:EdgeInsets.only(top: 40,left: 10,right: 10),
child: TextFormField(
textAlign: TextAlign.start,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
errorStyle: TextStyle(height: 0,color:Colors.transparent),
contentPadding: new EdgeInsets.only(top: 25.0,left: 100),
border: InputBorder.none,
prefixIcon: Padding(
padding: EdgeInsets.symmetric(vertical:20),
child: Icon(
Icons.email,
color: Colors.white,
),
),
hintText: 'Username',
//labelText: 'E-mail',
hintStyle: TextStyle(color:Colors.grey[400],
fontWeight: FontWeight.normal,
),
),
onSaved: (value) => {
_dLogin = User(
userName: value,
password: _editLogin.password,
createdAt: _editLogin.createdAt,
sId: _editLogin.sId,
)
},
validator: (value) {
if (value.length == 0)
return "";
else
return null;
},
),
),
Container(
decoration: myboxDecoration(),
height: 70,
width: double.infinity,
margin:EdgeInsets.only(top: 20,bottom: 10,left: 10,right: 10),
child: TextFormField(
textAlign: TextAlign.start,
style: TextStyle(
color: Colors.white
),
decoration: InputDecoration(
errorStyle: TextStyle(height: 0,color:Colors.transparent),
contentPadding: new EdgeInsets.only(top: 25.0),
border: InputBorder.none,
prefixIcon: Padding(
padding: EdgeInsets.symmetric(vertical:20),
child: Icon(
Icons.lock_outline,
color: Colors.white,
), // icon is 48px widget.
),
hintText: 'Password',
hintStyle: TextStyle(color:Colors.grey[400],
fontWeight: FontWeight.normal,
),
),
onSaved: (value) => {
_dLogin = User(
userName: _editLogin.userName,
password: value,
createdAt: _editLogin.createdAt,
sId: _editLogin.sId,
)
},
validator: (value) {
if (value.length == 0)
return "";
else if (value.length <= 5)
return "";
else
return null;
},
obscureText: true,
),
),
Container(
width: double.infinity,
height: 200
margin: EdgeInsets.only(left:20,right:20,top:50),
child: FlatButton(
textColor: Colors.white,
child:
Text('LOGIN',
style: TextStyle(
fontSize: 20.0,
),
),
onPressed: _saveNotification,
),
),
],
)
)
),
),
]
);
}
}
classloginhome扩展StatefulWidget{
@凌驾
_LoginHomeState createState()=>_LoginHomeState();
}
类_LoginHomeState扩展状态{
最终形式=GlobalKey();
var\u dLogin=用户(
createdAt:DateTime.now().toString(),
用户名:“”,
密码:“”,
Future\u savedetails()异步{
final isValid=_form.currentState.validate();
如果(!isValid){
返回;
}
);
_form.currentState.save();
等待提供程序.of(上下文,侦听:false).addLoginDetails(\u dLogin)
.catchError((错误){
showSnackBar(SnackBar(内容:Text('networkerror…'),);
}
);
@凌驾
小部件构建(构建上下文){
返回
纵队(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
容器(
子:SingleChildScrollView(
孩子:表格(
键:_形式,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
容器(
身高:70,
宽度:double.infinity,
页边距:仅限边集(顶部:40,左侧:10,右侧:10),
子项:TextFormField(
textAlign:textAlign.start,
样式:TextStyle(颜色:Colors.white),
装饰:输入装饰(
errorStyle:TextStyle(高度:0,颜色:颜色。透明),
contentPadding:仅限新边集(顶部:25.0,左侧:100),
边框:InputBorder.none,
前缀:填充(
填充:边缘组。对称(垂直:20),
子:图标(
Icons.email,
颜色:颜色,白色,
),
),
hintText:'用户名',
//labelText:“电子邮件”,
hintStyle:TextStyle(颜色:Colors.grey[400],
fontWeight:fontWeight.normal,
),
),
onSaved:(值)=>{
_dLogin=用户(
用户名:value,
密码:_editLogin.password,
createdAt:_editLogin.createdAt,
sId:_editLogin.sId,
)
},
验证器:(值){
如果(value.length==0)
返回“”;
其他的
返回null;
},
),
),
容器(
装饰:mybox装饰(),
身高:70,
宽度:double.infinity,
边距:仅限边集(顶部:20,底部:10,左侧:10,右侧:10),
子项:TextFormField(
textAlign:textAlign.start,
样式:TextStyle(
颜色:颜色。白色
),
装饰:输入装饰(
errorStyle:TextStyle(高度:0,颜色:颜色。透明),
contentPadding:仅限新边集(顶部:25.0),
边框:InputBorder.none,
前缀:填充(
填充:边缘组。对称(垂直:20),
子:图标(
Icons.lock_轮廓,
颜色:颜色,白色,
),//图标是48px小部件。
),
hintText:'密码',
hintStyle:TextStyle(颜色:Colors.grey[400],
fontWeight:fontWeight.normal,
),
),
onSaved:(值)=>{
_dLogin=用户(
用户名:_editLogin.userName,
密码:value,
createdAt:_editLogin.createdAt,
sId:_editLogin.sId,
)
},
验证器:(值){
如果(value.length==0)
返回“”;
else if(value.length从代码中,我看到您需要在map['user']上为每个循环进行迭代,而不仅仅是在map上,这将为您提供内部对象数据。我只是在编写它,只是通过在foreach循环中打印值进行检查。我没有在笔记本电脑上进行检查,但很快就会进行检查
只需检查编辑,我举了一个示例,您的foreach循环有问题
String response = '''{
"user": {
"username": "Tom",
"__v": 82
},
"token": "eyJhbGciOiJIUzICJ9.eyJfaWQiOiI1ZjA1NmY5YWI3NNDc1MDF9.zxii8_5DX9dSg-htXXbPfz0qKDI1"
}''';
@override
initState() {
super.initState();
Map item = json.decode(response);
print(item['token']);
item['user'].forEach((key, value) {
print(key);
print(value);
});
}
只需检查这两个参数iV和Sid,如果它们是int,并且您调用的字符串参数是integer,只需将其更改为toString()
class LoginHome extends StatefulWidget {
@override
_LoginHomeState createState() => _LoginHomeState();
}
class _LoginHomeState extends State<LoginHome> {
final _form = GlobalKey<FormState>();
var _dLogin = User(
createdAt: DateTime.now().toString(),
userName: '',
password: '',
Future<void> _savedetails() async{
final isValid = _form.currentState.validate();
if(!isValid) {
return;
}
);
_form.currentState.save();
await Provider.of<LoginProvider>(context, listen: false).addLoginDetails(_dLogin)
.catchError((error) {
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Network error...'),));
}
);
@override
Widget build(BuildContext context) {
return
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
Container(
child: SingleChildScrollView(
child: Form(
key:_form,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
height: 70,
width: double.infinity,
margin:EdgeInsets.only(top: 40,left: 10,right: 10),
child: TextFormField(
textAlign: TextAlign.start,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
errorStyle: TextStyle(height: 0,color:Colors.transparent),
contentPadding: new EdgeInsets.only(top: 25.0,left: 100),
border: InputBorder.none,
prefixIcon: Padding(
padding: EdgeInsets.symmetric(vertical:20),
child: Icon(
Icons.email,
color: Colors.white,
),
),
hintText: 'Username',
//labelText: 'E-mail',
hintStyle: TextStyle(color:Colors.grey[400],
fontWeight: FontWeight.normal,
),
),
onSaved: (value) => {
_dLogin = User(
userName: value,
password: _editLogin.password,
createdAt: _editLogin.createdAt,
sId: _editLogin.sId,
)
},
validator: (value) {
if (value.length == 0)
return "";
else
return null;
},
),
),
Container(
decoration: myboxDecoration(),
height: 70,
width: double.infinity,
margin:EdgeInsets.only(top: 20,bottom: 10,left: 10,right: 10),
child: TextFormField(
textAlign: TextAlign.start,
style: TextStyle(
color: Colors.white
),
decoration: InputDecoration(
errorStyle: TextStyle(height: 0,color:Colors.transparent),
contentPadding: new EdgeInsets.only(top: 25.0),
border: InputBorder.none,
prefixIcon: Padding(
padding: EdgeInsets.symmetric(vertical:20),
child: Icon(
Icons.lock_outline,
color: Colors.white,
), // icon is 48px widget.
),
hintText: 'Password',
hintStyle: TextStyle(color:Colors.grey[400],
fontWeight: FontWeight.normal,
),
),
onSaved: (value) => {
_dLogin = User(
userName: _editLogin.userName,
password: value,
createdAt: _editLogin.createdAt,
sId: _editLogin.sId,
)
},
validator: (value) {
if (value.length == 0)
return "";
else if (value.length <= 5)
return "";
else
return null;
},
obscureText: true,
),
),
Container(
width: double.infinity,
height: 200
margin: EdgeInsets.only(left:20,right:20,top:50),
child: FlatButton(
textColor: Colors.white,
child:
Text('LOGIN',
style: TextStyle(
fontSize: 20.0,
),
),
onPressed: _saveNotification,
),
),
],
)
)
),
),
]
);
}
}
String response = '''{
"user": {
"username": "Tom",
"__v": 82
},
"token": "eyJhbGciOiJIUzICJ9.eyJfaWQiOiI1ZjA1NmY5YWI3NNDc1MDF9.zxii8_5DX9dSg-htXXbPfz0qKDI1"
}''';
@override
initState() {
super.initState();
Map item = json.decode(response);
print(item['token']);
item['user'].forEach((key, value) {
print(key);
print(value);
});
}