尝试将用户登录到应用程序时,我遇到了这个问题。
登录详细信息将发送到服务器,之后 必要的身份验证,结果被发送回用户(仅用户名和生成的令牌被发送回用户-仅需要此数据), 但是在运行forEach插入数据时,它会抛出错误类型'String'不是'index'类型'int'的子类型,无法查明造成此问题的确切行>
如果有人可以帮助我解决此问题。
Login.dart
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;
}
}
Provider.dart
class LoginProvider with ChangeNotifier {
List<Login> _loginDetails = [];
List<Login> get items {
return [..._loginDetails];
}
Future<void> addLoginDetails(User loginDetailscheck ) async {
print("reached");
Map<String, String> headers = {
"Content-Type": "charset=utf-8",
"Content-type": "application/json"};
const url = 'http://localhost:3000/login/cred';
try
{
var response = await http.post(url, headers: headers,body:
json.encode({
"user_name": loginDetailscheck.userName,
"password": loginDetailscheck.password,
"created_at": loginDetailscheck.createdAt,
"_id":loginDetailscheck.sId
})
);
final map = json.decode(response.body) as Map<String,dynamic>;
final List<Login> loadData = [];
map.forEach((prodId, datacheck) {
loadData.add( LoginPage(
user: User(
userName: datacheck['username'],
),
token: json.decode(response.body)['token'],
)
);
print(datacheck['username']);
print(json.decode(response.body)['token']);
});
if(response.statusCode == 201) {
_loginDetails = loadData;
print(json.decode(response.body));
notifyListeners();
}else{
print('invalid data ');
}
}catch(error) {
throw (error);
}
}
}
收到的数据格式:
{
"user": {
"username": "Tom",
"__v": 82
},
"token": "eyJhbGciOiJIUzICJ9.eyJfaWQiOiI1ZjA1NmY5YWI3NNDc1MDF9.zxii8_5DX9dSg-htXXbPfz0qKDI1"
}
ui.dart
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,
),
),
],
)
)
),
),
]
);
}
}
答案 0 :(得分:1)
从代码中,我看到您需要对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);
});
}