【发布时间】:2020-01-25 09:42:18
【问题描述】:
当我点击我的SwitchButton 时,我将ID 保存在SharedPreferences 中。
我恢复了这个 id 并请求了我的 API。
因为有了这个ID,我恢复了所有的技术。
但问题是,我的列表在每个 id 处都被重置了。
编辑:
Theme theme;
List<Technology> technos = [];
class Technology {
int id;
String name;
Technology({this.id, this.name});
factory Technology.fromJson(Map<String, dynamic> json) {
return Technology(
id: json['id'],
name: json['name'],
);
}
Future<List<Technology>> getTechnologies() async {
String id = await Theme().getTheme();
String url = 'http://10.0.2.2:3000/v1/api/theme/${id}/technology';
String token = await Candidate().getToken();
final response = await http.get(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ${token}',
});
if (response.statusCode == 200) {
List technosList = jsonDecode(response.body);
for (var technoMap in technosList) {
technos.add(Technology.fromJson(technoMap));
}
return technos;
} else {
throw Exception('Failed to load technos');
}
}
}
【问题讨论】:
-
这是否意味着
technos列表总是只有一个条目?您可以打印technoList的大小以确保它正在被重置吗? -
我打印了我的 id 字符串和技术专家:1 I/flutter(7137):2 I/flutter(7137):id:2 I/flutter(7137):[{id:1,名称: GitHub}, {id: 2, name: BitBucket}, {id: 3, name: Travis}, {id: 4, name: Docker}] I/flutter (7137): Dans le var: [{id: 1, name: GitHub}, {id: 2, name: BitBucket}, {id: 3, name: Travis}, {id: 4, name: Docker}] 我们可以看到,technolist 没有 id 2 的数据 @ChennaReddy
标签: android mobile flutter flutter-layout