简体   繁体   English

当我请求我的 API 在 map flutter 中存储数据时出现问题

[英]Problem when i request my API for store data in map flutter

When i clicked in my SwitchButton , i save the id in SharedPreferences .当我点击我的SwitchButton时,我将 ID 保存在SharedPreferences中。 I recupered this id and i requested my API.我恢复了这个 ID,并请求了我的 API。

Because with this ID, i recupered all technologies.因为有了这个ID,我恢复了所有的技术。

But the problem, my list is reset at each id.但问题是,我的列表在每个 id 处被重置。

EDIT:编辑:


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');
    }
  }
}


Because at each API you are overwriting technos hence previous data is lost.因为在每个technos上,您都在覆盖技术,因此以前的数据会丢失。 You can make this technos global on that Screen or if you use BLOC methods.您可以在该Screen上将此technos全局,或者如果您使用BLOC方法。

Use List<Technology> technos = [];使用List<Technology> technos = []; globally not in getTechnologies() .全球不在getTechnologies()中。

rest is ok to add rest 可以加

technos.add(Technology.fromJson(technoMap));

I edited my method:我编辑了我的方法:

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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在向 Google Map API 发出 http 请求时,我可以使用我的 Android API 密钥吗? - Can I use my Android API key when making http request to Google Map API? 当我想在 Flutter 中调试我的应用程序时遇到问题 - I have problem when I want debug my app in Flutter 如何在我的活动中存储来自API的数据 - How can I store data from API in my activity 无法使用 flutter 向我的 api 发送发布请求 - Cant send a post request to my api with flutter 当我运行我的代码时,它会给出错误代码 400,但是当我进行热刷新时,它会在控制台中显示数据,同时从 flutter 中的 api 获取数据 - when i run my code it gives error code 400 but when i do hot refresh it shows data in console while getting data from api in flutter 我在安装 flutter 时遇到问题 - I have a problem when I was installing flutter 当我尝试在我的 flutter 应用程序中使用 firebase_Auth 时出现问题 - Problem occurs when i'm trying to use firebase_Auth in my flutter app 当我从Play商店下载应用程序时,为什么Google地图没有显示? - Why google map is not displaying when I download my application from play store? 当我更改api级别时,我的http请求不起作用 - When i change my api level my http request doesn't work 如何从 Prestashop API 检索更多数据到我的 flutter 应用程序? - How can I retrieve more data from Prestashop API to my flutter app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM