我有这个Json列表,应该在listview中,我试图调用Api,它返回数组对象的每个值,但是当我在命令行中打印出结果时,它仅显示json中的1个值,
为什么我尝试解码数组值中仅显示一个值的数据
这就是杰森
{
"Status": 1,
"Membership_Plans": [
{
"Is_Membership_Activated": true,
"Status": 1,
"_id": "5cc2c6d92e444a62fcfadcb1",
"Membership_Amount": "7000",
"Membership_Duration_In_Months": 6,
"Registration_Date": "1556236800",
"__v": 0
},
{
"Is_Membership_Activated": true,
"Status": 1,
"_id": "5cc2c6e32e444a62fcfadcb2",
"Membership_Amount": "12000",
"Membership_Duration_In_Months": 12,
"Registration_Date": "1556236800",
"__v": 0
},
{
"Is_Membership_Activated": true,
"Status": 1,
"_id": "5dca5d5cfc150c7e2bbb84d2",
"Membership_Amount": "4000",
"Membership_Duration_In_Months": 3,
"Registration_Date": "1573543251",
"__v": 0
}
]
}
用于检索Api的功能
Future<String> GetDetails() async {
Future notificatinstatus = SharedPrefrence().getuserId();
notificatinstatus.then((data) async {
var userid = data;
print(userid);
var response = await http.post(Urls.Gym_Validate,
headers: {"Content-Type": "application/json"},
body: json.encode({"Id_To_Validate": userid}));
print('Respone: ${response.body}');
try {
Map<String, dynamic> value = json.decode(response.body);
var status = value['Status'].toString();
var details = value['Membership_Plans'][0];
var Duration = details["Membership_Duration_In_Months"];
var Amount = details["Membership_Amount"];
print(Duration);
print(Amount);
} catch (e) {
e.toString();
}
});
}
尝试一下。将索引放在第一位。
Future<String> GetDetails() async {
Future notificatinstatus = SharedPrefrence().getuserId();
notificatinstatus.then((data) async {
var userid = data;
print(userid);
var response = await http.post(Urls.Gym_Validate,
headers: {"Content-Type": "application/json"},
body: json.encode({"Id_To_Validate": userid}));
print('Respone: ${response.body}');
try {
Map<String, dynamic> value = json.decode(response.body);
var status = value['Status'].toString();
var details = value[0]['Membership_Plans'];
var Duration = details[0]["Membership_Duration_In_Months"];
var Amount = details[0]["Membership_Amount"];
print(Duration);
print(Amount);
} catch (e) {
e.toString();
}
});
}