Android 请求API在map Flatter中存储数据时出现问题

Android 请求API在map Flatter中存储数据时出现问题,android,mobile,flutter,flutter-layout,Android,Mobile,Flutter,Flutter Layout,当我点击我的开关按钮时,我将id保存在共享参考文件中。 我重复了这个id,并请求了我的API 因为有了这个ID,我重复了所有的技术 但问题是,我的列表会在每个id处重置 编辑: Theme theme; List<Technology> technos = []; class Technology { int id; String name; Technology({this.id, this.name}); factory Technology.fromJs

当我点击我的
开关按钮时,我将id保存在
共享参考文件中。
我重复了这个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=[];
课堂技术{
int-id;
字符串名;
技术({this.id,this.name});
factory Technology.fromJson(映射json){
返回技术(
id:json['id'],
名称:json['name'],
);
}
未来的getTechnologies()异步{
String id=wait Theme().getTheme();
字符串url='0http://10.0.2.2:3000/v1/api/theme/${id}/技术';
字符串标记=等待候选者().getToken();
最终响应=等待http.get(url,标题:{
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
'Authorization':'Bearer${token}',
});
如果(response.statusCode==200){
List technosList=jsonDecode(response.body);
用于(technosList中的var technoMap){
add(Technology.fromJson(technoMap));
}
返回技术;
}否则{
抛出异常(“加载技术失败”);
}
}
}

因为在每个API上您都在覆盖
技术
,因此以前的数据会丢失。 您可以在
屏幕上或使用
BLOC
方法将此
technos
设置为全局

使用
List technos=[]全局不在
getTechnologies()

其余的可以添加

technos.add(Technology.fromJson(technoMap));
我编辑了我的方法:

Future<List<String>> getListTheme() async {
    List<String> themes = List<String>();
    final SharedPreferences preferences = await SharedPreferences.getInstance();
    for (String key in preferences.getKeys()) {
      if (key == 'token') {
        preferences.containsKey(key);
      } else {
        themes.add(jsonEncode(preferences.getStringList(key)));
      }
    }
    return themes;
  }

Future getListTheme()异步{
列表主题=列表();
最终SharedReferences首选项=等待SharedReferences.getInstance();
for(首选项中的字符串键。getKeys()){
如果(键=='token'){
首选项。containsKey(键);
}否则{
添加(jsonEncode(preferences.getStringList(key));
}
}
返回主题;
}

这是否意味着
technos
列表总是只有一个条目?你能打印
technoList
的大小以确保它被重置吗?我打印了我的id字符串和technoList:1i/flatter(7137):2i/flatter(7137):id:2i/flatter(7137):[{id:1,name:GitHub},{id:2,name:BitBucket},{id:3,name:Travis},{id:4,name:Docker}]i/flatter(7137):Dans-le-var:{id:1,name:GitHub},{id:2,name:BitBucket},{id:3,name:Travis},{id:4,name:Docker}]我们可以看到,technolist没有id 2@Chennareddyes的数据,我知道,我测试了这个,但它不起作用…我没有id 1或id 2的techno,这取决于我在第一次点击时我编辑了我的帖子,在我的结果中,我一直在我的列表中id 2的数据仍然是错误的,这里不应该是全局的,但在UI页面中你应该有添加新结果的列表
List.add(getTechnologies());setstate();