简体   繁体   English

API 当屏幕关闭(或应用程序进入后台)时调用终止 IOS Flutter

[英]API call terminate when screen gets off (or app goes background) IOS Flutter

I have a login page which starts downloading base data for the app after user enters username and password, and its a long duration operation like 2 or 3 minutes我有一个登录页面,在用户输入用户名和密码后开始下载应用程序的基本数据,并且它的操作持续时间很长,例如 2 或 3 分钟

In IOS at the middle of downloading data if screen gets off and locked, the operations terminates. IOS 在下载数据的过程中如果屏幕关闭并锁定,则操作终止。

Here is the code LoginPage part:这是代码 LoginPage 部分:

var repository = GlobalRestRepository();
var db = BasicDB();
List<basicModel> notDownloaded = await db.selectByLoaded(false);
for (int i = 0; i < notDownloaded.length; i++) {
    await repository.getBasic(notDownloaded.elementAt(i));
}

GlobalRestRepository part: GlobalRestRepository 部分:

class GlobalRestRepository {
  final HttpClient http = HttpClient();

Future<void> getBasic(basicModel model) async {
    String url = "${Variables.mainUrl + basicModelUrl}";

    var response = await http.postExtraToken(url);
    .
    .
    .
 }
}

HttpClient part: HttpClient部分:

import 'package:http/http.dart';
...
class HttpClient {

  static final HttpClient _instance = HttpClient._privateConstructor();

  factory HttpClient() {
    return _instance;
  }

  Future<dynamic> postExtraToken(String path) async {
    Response response;
    try {
      response = await post(Uri.parse(path),
              headers: {"extra": Variables.extra, "token": Variables.token});
      final statusCode = response.statusCode;
      if (statusCode >= 200 && statusCode < 299) {
        if (response.body.isEmpty) {
          return [];
        } else {
          return jsonDecode(utf8.decode(response.bodyBytes));
        }
      } else if (statusCode >= 400 && statusCode < 500) {
        throw ClientErrorException();
      } else if (statusCode >= 500 && statusCode < 600) {
        throw ServerErrorException();
      } else {
        throw UnknownException();
      }
    } on SocketException {
      throw ConnectionException();
    }
  }
}

Can anyone help me with this?谁能帮我这个?

Using Wakelock plugin, we can solve this problem, but I don't this is the best solution or not.使用Wakelock插件,我们可以解决这个问题,但我不认为这是最好的解决方案。 Note: This plugin works for all the platforms except linux.注意:此插件适用于除 linux 以外的所有平台。

Add the below code in the screen where you are initiating the api request.在您启动 api 请求的屏幕中添加以下代码。

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    checkAndEnableWakeLock();
  }

  void checkAndEnableWakeLock() async {
    bool wakeLockEnabled = await Wakelock.enabled;
    if (!wakeLockEnabled) {
      Wakelock.enable();
    }
  }

Call disable method when all the api calls are completed.当所有 api 调用完成时调用禁用方法。

Wakelock.disable();

Using above code you can avoid screen getting off issue.使用上面的代码可以避免屏幕脱落问题。

暂无
暂无

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

相关问题 当应用在 Android 和 iOS 上离开屏幕时执行代码 - Execute code when app goes off-screen on Android & iOS Flutter:如何解决 ios 应用程序在后台时 callkeep 不显示来电屏幕的问题? - Flutter: how to fix callkeep is not showing incoming call screen when the app is in background in ios? Flutter:声明 xcode 音频背景模式以在屏幕关闭但 ios 被拒绝时保持套接字通道(颤振)运行 - Flutter: Declare xcode audio background mode to keep socket channel (flutter) running when screen off but ios rejected 当应用程序进入后台时,在 iOS 设备的 Flutter 应用程序上发布请求失败 - Post request failing on flutter app for iOS device when app goes to background Flutter:当应用程序在后台时,如何在 ios 上显示全屏来电? - Flutter: How to display fullscreen incoming call on ios when the app is in background? 当应用程序进入后台时,Flutter CamerPlugin“相机预览”冻结 - Flutter CamerPlugin “camera preview” freezed when app goes to background Flutter:当应用程序处于后台但未在 iOS 中终止时,无法在通知单击时重定向到特定屏幕 - Flutter : Unable to redirect to specific screen on Notification click when app is in background but not terminated in iOS 当应用程序在后台flutter时调用onMessage方法 - Call onMessage method when the app is in background in flutter Flutter 应用程序在互联网关闭时显示灰屏(在调试 apk 中) - Flutter App Showing Grey Screen when internet is off(in debug apk) 当应用程序在后台或终止时,推送通知未进入 IOS 设备。 在 Flutter 的 IOS 设备中导航到下一个屏幕也不起作用? - Push notification not coming in IOS device when app in background or kill. Navigate to next screen also not working In IOS device in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM