initState调用函数两次

问题描述 投票:0回答:0

我试图在底部导航栏主页的 initState 中调用 Api 函数,但是当我安装应用程序并登录并转到 initState 中的主页 api 函数时,它被调用了两次。

这是我的代码,

GetRecordApiCall(context) async {
  // Call your API here and pass `_verificationCode` as a parameter
  final api = ApiServices(Dio(), "/getRecord");
  // Api response will get here
  final response = await api.ApiUser(toJson(), context);
  //Print response here
  print('Response status: ${response.statusCode}');
  print('Response body: ${response}');
  print(
      'Response headers: ${response.headers[HttpHeaders.authorizationHeader]}');
  // New token will be saved here
  String decodeResponse = response.toString();
  Map<String, dynamic> res = jsonDecode(decodeResponse);
  res['token'] = '${response.headers[HttpHeaders.authorizationHeader]}';
  late var token = res['token'];
  token = token.substring(1, token.length - 1);
  print(token);
  // Shared preferences used here so we can store token which will get from api.
  SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  sharedPreferences.setString('token', token);

  print("Get Record request send to api");

}


  @override
  void initState() {
    super.initState();
    GetRecordApiCall(context);
  }
flutter function api init dio
© www.soinside.com 2019 - 2024. All rights reserved.