[英]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.