我使用 api 调用创建 flutter,但数据在全新安装时未显示

发布于 2025-01-12 18:58:40 字数 1712 浏览 0 评论 0原文

因此,我使用 REST API 创建一个应用程序,但数据在全新安装时未显示

这是用于 gettoken 并保存到共享首选项

getInit() async {
    String myUrl = "$serverUrl/get-token";
    http.Response response = await http.post(Uri.parse(myUrl),
        body: {'secret': 'code'});
    debugPrint(response.statusCode.toString());
    debugPrint(response.body);

    var data = json.decode(response.body)["data"];
    _save(data["access_token"]);
    // return data;
  }

  //SAVE TOKEN
  _save(String token) async {
    final prefs = await SharedPreferences.getInstance();
    const key = 'token';
    final value = token;

    prefs.setString(key, value);
    debugPrint("new token save " + value);
  }

这是用于 getlist 项目,需要来自共享首选项的不记名访问令牌

getRecList() async {
    final prefs = await SharedPreferences.getInstance();
    const key = 'token';
    final value = prefs.get(key) ?? 0;

    String myUrl = "$serverUrl/home";
    http.Response response = await http.get(Uri.parse(myUrl), headers: {
      'Accept': 'application/json',
      'Authorization': 'Bearer $value'
    });
    debugPrint(response.body);

    if (response.statusCode == 200) {
      List data = jsonDecode(response.body)['data'];
      List<ModelKost> modelkost =
          data.map((item) => ModelKost.fromJson(item)).toList();
      return modelkost;
    } else {
      return <ModelKost>[];
    }
  }

所以每次我全新安装时,主页都不会显示显示任何数据,因为 getRecList 项目被禁止访问...

日志显示令牌成功,但 getRecList 失败,因为没有获取访问令牌,只有在刷新/热重新加载正常显示的列表时才会发生这种情况...

所以我猜函数 getRecList 错误在这里,但我不知道修复它...

在此处输入图像描述

So I create an app with rest API, but the data not showing on a fresh install

This is for gettoken and save to shared prefs

getInit() async {
    String myUrl = "$serverUrl/get-token";
    http.Response response = await http.post(Uri.parse(myUrl),
        body: {'secret': 'code'});
    debugPrint(response.statusCode.toString());
    debugPrint(response.body);

    var data = json.decode(response.body)["data"];
    _save(data["access_token"]);
    // return data;
  }

  //SAVE TOKEN
  _save(String token) async {
    final prefs = await SharedPreferences.getInstance();
    const key = 'token';
    final value = token;

    prefs.setString(key, value);
    debugPrint("new token save " + value);
  }

This for getlist item, need bearer access token from shared prefs

getRecList() async {
    final prefs = await SharedPreferences.getInstance();
    const key = 'token';
    final value = prefs.get(key) ?? 0;

    String myUrl = "$serverUrl/home";
    http.Response response = await http.get(Uri.parse(myUrl), headers: {
      'Accept': 'application/json',
      'Authorization': 'Bearer $value'
    });
    debugPrint(response.body);

    if (response.statusCode == 200) {
      List data = jsonDecode(response.body)['data'];
      List<ModelKost> modelkost =
          data.map((item) => ModelKost.fromJson(item)).toList();
      return modelkost;
    } else {
      return <ModelKost>[];
    }
  }

So every time I fresh install, home page does not show any data because getRecList item is forbidden access...

The log says token success, but getRecList fails because not get access token, it only happens on fresh install if I refresh/hot reload the list showing normally ...

so I guess the function getRecList wrong here, but I have no idea to fix it ...

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2025-01-19 18:58:40

我认为问题是你没有等待代币价值。从共享首选项中获取价值时使用 await

So I create an app with rest API, but the data not showing on a fresh install



getRecList() async {
    final prefs = await SharedPreferences.getInstance();
    const key = 'token';
    final value =await  prefs.get(key) ?? 0; //use await here

    String myUrl = "$serverUrl/home";
    http.Response response = await http.get(Uri.parse(myUrl), headers: {
      'Accept': 'application/json',
      'Authorization': 'Bearer $value'
    });
    debugPrint(response.body);

    if (response.statusCode == 200) {
      List data = jsonDecode(response.body)['data'];
      List<ModelKost> modelkost =
          data.map((item) => ModelKost.fromJson(item)).toList();
      return modelkost;
    } else {
      return <ModelKost>[];
    }
  }

i think the problem is you are not waiting for token value. use await when geting value from shared preferences

So I create an app with rest API, but the data not showing on a fresh install



getRecList() async {
    final prefs = await SharedPreferences.getInstance();
    const key = 'token';
    final value =await  prefs.get(key) ?? 0; //use await here

    String myUrl = "$serverUrl/home";
    http.Response response = await http.get(Uri.parse(myUrl), headers: {
      'Accept': 'application/json',
      'Authorization': 'Bearer $value'
    });
    debugPrint(response.body);

    if (response.statusCode == 200) {
      List data = jsonDecode(response.body)['data'];
      List<ModelKost> modelkost =
          data.map((item) => ModelKost.fromJson(item)).toList();
      return modelkost;
    } else {
      return <ModelKost>[];
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文