android - 奇怪的应用程序行为 我的应用程序有时只是行为异常
问题描述
所以最近我刚刚使用颤振制作了我的第一个应用程序。该应用程序是关于穆斯林祈祷时间表。
我自己制作了这个应用程序,当我遇到这样的问题时没有人支持我。希望有人可以帮助我解决这个问题。
所以方法是从 Web API 收集计划数据并将其编码为 Json 模式到我的应用程序,但有时我的应用程序只是卡在加载屏幕上,不会转到另一个屏幕/用户界面。即使我已经检查了互联网连接,如果互联网连接在某个时间跨度很慢,使用超时方法转到另一个屏幕,如果连接失败,有时它就不会去。
这个问题实际上很少发生,但最终会导致问题并降低用户对应用程序的体验。这是我认为有问题的代码:
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:intl/intl.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:geolocator/geolocator.dart' as geo;
import 'package:yaumi/services/shared/preferenceServices.dart';
//import 'package:location/location.dart';
class FirstLoad extends StatefulWidget {
const FirstLoad();
@override
_FirstLoadState createState() => _FirstLoadState();
}
class _FirstLoadState extends State<FirstLoad> {
final geo.GeolocatorPlatform _geolocatorPlatform = geo.GeolocatorPlatform.instance;
bool serviceEnabled;
geo.LocationPermission _permission;
geo.Position _currentPosition;
//Location _location;
var lat;
var lon;
var ele;
var date;
bool isApiSatu = true;
bool isConnected = true;
final String tahun = DateTime.now().year.toString();
final String bulan = DateTime.now().month.toString();
final String tanggal = DateTime.now().day.toString();
String sharedShubuh= '';
String sharedDhuhur= '';
String sharedAshar= '';
String sharedMaghrib= '';
String sharedIsya= '';
void setupsWaktuShalat() async {
//serviceEnabled = await location.serviceEnabled();
// if (!serviceEnabled) {
// serviceEnabled = await location.requestService();
// if (!serviceEnabled) {
// return print('Service Permission is not given');
// }
// }
//TODO: Untuk HP China seperti Xiaomi atau Huawei biasanya harus setting manual dulu pengaturan batterai nya
// _permissionGranted = await location.hasPermission();
// if (_permissionGranted == PermissionStatus.denied) {
// _permissionGranted = await location.requestPermission();
// if (_permissionGranted != PermissionStatus.granted) {
// return print('Permission request not granted');
// }
// }
serviceEnabled = await _geolocatorPlatform.isLocationServiceEnabled();
if(!serviceEnabled) {
return print('_geolocator service not enabled');
}
_permission = await _geolocatorPlatform.checkPermission();
if(_permission==geo.LocationPermission.denied){
_permission = await _geolocatorPlatform.requestPermission();
if (_permission==geo.LocationPermission.denied) {
return print('permission not granted');
}
}
await geo.Geolocator
.getCurrentPosition(desiredAccuracy: geo.LocationAccuracy.medium, forceAndroidLocationManager: true)
.timeout(Duration(seconds: 5))
.then((geo.Position position) {
setState(() {
//print('position: $position');
_currentPosition = position;
print('_currentPosition: $_currentPosition');
});
}).catchError((e) {
print(e);
});
if (_currentPosition != null) {
lat = _currentPosition.latitude;
lon = _currentPosition.longitude;
} else {
lat = '-6.864008';
lon = '107.591192';
}
//print('$lat and $lon');
ele = 333;
date = DateFormat('yyyy-MM-dd').format(new DateTime.now());
//Navigator.pushReplacementNamed(context, '/home');
try {
final result = await InternetAddress.lookup('example.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
//print('connected');
isConnected = true;
}
} on SocketException catch (_) {
//print('not connected');
isConnected = false;
}
if (isConnected) {
try {
Response response =
await get(
Uri.parse(
'https://api.pray.zone/v2/times/day.json?longitude=$lon&latitude=$lat&elevation=$ele&date=$date'
)
).timeout(Duration(seconds: 10));
Map map = jsonDecode(response.body);
//print(map);
Map _results = map['results'];
//print(_results);
List _datetimeList = _results['datetime'];
Map _datetime = _datetimeList[0];
Map _times = _datetime['times'];
setState(() {
sharedShubuh = _times['Fajr'];
sharedDhuhur = _times['Dhuhr'];
sharedAshar = _times['Asr'];
sharedMaghrib = _times['Maghrib'];
sharedIsya = _times['Isha'];
});
try {
await PreferenceServices.setShubuh(sharedShubuh);
await PreferenceServices.setDhuhur(sharedDhuhur);
await PreferenceServices.setAshar(sharedAshar);
await PreferenceServices.setMaghrib(sharedMaghrib);
await PreferenceServices.setIsya(sharedIsya);
} catch (e) {
print('error karena $e');
}
//print(_times);
Navigator.pushReplacementNamed(context, '/home', arguments: {
'Shubuh': sharedShubuh,
'Dhuhur': sharedDhuhur,
'Ashar': sharedAshar,
'Maghrib': sharedMaghrib,
'Isya': sharedIsya,
'Connection': true
});
} catch (e) {
try {
setState(() {
isApiSatu = false;
});
Response response =
await get(
Uri.parse(
'https://api.myquran.com/v1/sholat/jadwal/1219/$tahun/$bulan/$tanggal'
)
).timeout(Duration(seconds: 10));
Map map = jsonDecode(response.body);
print(map);
Map _data = map['data'];
Map _jadwal = _data['jadwal'];
print(_jadwal);
setState(() {
sharedShubuh = _jadwal['subuh'];
sharedDhuhur = _jadwal['dzuhur'];
sharedAshar = _jadwal['ashar'];
sharedMaghrib = _jadwal['maghrib'];
sharedIsya = _jadwal['isya'];
});
Navigator.pushReplacementNamed(context, '/home', arguments: {
'Shubuh': sharedShubuh,
'Dhuhur': sharedDhuhur,
'Ashar': sharedAshar,
'Maghrib': sharedMaghrib,
'Isya': sharedIsya,
'Connection': true
});
} catch (e) {
print(e);
Navigator.pushReplacementNamed(context, '/home', arguments: {
'Shubuh': sharedShubuh,
'Dhuhur': sharedDhuhur,
'Ashar': sharedAshar,
'Maghrib': sharedMaghrib,
'Isya': sharedIsya,
'Connection': true
});
}
}
} else {
//print('sharedDhuhur:$sharedDhuhur');
Navigator.pushReplacementNamed(context, '/home', arguments: {
'Shubuh': sharedShubuh,
'Dhuhur': sharedDhuhur,
'Ashar': sharedAshar,
'Maghrib': sharedMaghrib,
'Isya': sharedIsya,
'Connection': false
});
}
}
@override
void initState() {
//Navigator.pushReplacementNamed(context, '/home');
PreferenceServices.getShubuh().then((e) =>
sharedShubuh = e.toString()
);
PreferenceServices.getDhuhur().then((e) =>
sharedDhuhur = e.toString()
);
PreferenceServices.getAshar().then((e) =>
sharedAshar = e.toString()
);
PreferenceServices.getMaghrib().then((e) =>
sharedMaghrib = e.toString()
);
PreferenceServices.getIsya().then((e) =>
sharedIsya = e.toString()
);
setupsWaktuShalat();
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Color(0xFF6441A5), Color(0xFF2a0845)]),
),
child: Center(
child: SpinKitFadingCube(
color: Colors.white,
size: 50.0,
),
),
);
}
}
这是我的应用程序的链接,用于进一步检查应用程序:
解决方案
推荐阅读
- .net - 如何从 .NET Framework 构建的项目中引用 .NET Core 构建的项目?
- c++ - 有人可以帮助修复 C++ 二叉搜索树编译失败吗?
- c - 元素不会从堆栈中移除
- javascript - 将 Python 字典转换为没有字符串键的 JSON 对象
- python - 如何使该图具有从 0 递增到 19 的 ax 轴?
- r - 查找列的平均值时出错,错误项:“参数不是数字或逻辑:返回 NA[1] NA”
- ubuntu - 在 ubuntu 上恢复 clion 默认设置
- javascript - 如何使用用户输入修改数组(用 html 中的表格显示)
- php - Symfony POST Value Id 转换为实体
- django - Проблема с файлом wsgi.py(wsgi.py 文件的问题)