fixed context across async gaps
This commit is contained in:
@@ -121,14 +121,15 @@ class _AddCamMainState extends State<AddCamMain> {
|
||||
|
||||
// determine live position with checks for denied permission and turned off location service
|
||||
Future<Position> _deteterminePosition() async {
|
||||
|
||||
|
||||
|
||||
bool locationEnabled;
|
||||
LocationPermission permissionGiven;
|
||||
|
||||
locationEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!locationEnabled) {
|
||||
// ignore: use_build_context_synchronously
|
||||
if (!locationEnabled && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
// ignore: use_build_context_synchronously
|
||||
content: Text(AppLocalizations.of(context)!.locationDisabled)));
|
||||
return currentPosition;
|
||||
}
|
||||
@@ -136,20 +137,16 @@ class _AddCamMainState extends State<AddCamMain> {
|
||||
permissionGiven = await Geolocator.checkPermission();
|
||||
if (permissionGiven == LocationPermission.denied) {
|
||||
permissionGiven = await Geolocator.requestPermission();
|
||||
if (permissionGiven == LocationPermission.denied) {
|
||||
// ignore: use_build_context_synchronously
|
||||
if (permissionGiven == LocationPermission.denied && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
// ignore: use_build_context_synchronously
|
||||
content: Text(AppLocalizations.of(context)!.locationForbidden)));
|
||||
|
||||
return currentPosition;
|
||||
}
|
||||
}
|
||||
|
||||
if (permissionGiven == LocationPermission.deniedForever) {
|
||||
// ignore: use_build_context_synchronously
|
||||
if (permissionGiven == LocationPermission.deniedForever && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
// ignore: use_build_context_synchronously
|
||||
content: Text(AppLocalizations.of(context)!.locationForbidden)));
|
||||
return currentPosition;
|
||||
}
|
||||
@@ -275,7 +272,6 @@ class _AddCamMainState extends State<AddCamMain> {
|
||||
onPressed: () async {
|
||||
saveTemplate();
|
||||
Navigator.pushNamedAndRemoveUntil(
|
||||
// ignore: use_build_context_synchronously
|
||||
context,
|
||||
'/home',
|
||||
(route) => false);
|
||||
@@ -320,14 +316,12 @@ class _AddCamMainState extends State<AddCamMain> {
|
||||
onPressed: () async {
|
||||
saveData();
|
||||
saveFile();
|
||||
// ignore: use_build_context_synchronously
|
||||
},
|
||||
child: Text(AppLocalizations.of(context)!.saveasfile)),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
saveData();
|
||||
Navigator.pushNamedAndRemoveUntil(
|
||||
// ignore: use_build_context_synchronously
|
||||
context,
|
||||
'/home',
|
||||
(route) => false);
|
||||
@@ -360,36 +354,34 @@ class _AddCamMainState extends State<AddCamMain> {
|
||||
String jsonPlace = jsonEncode(place);
|
||||
|
||||
if (selectedDirectory == null) {
|
||||
// ignore: use_build_context_synchronously
|
||||
Navigator.pop(context);
|
||||
if (mounted) Navigator.pop(context);
|
||||
return;
|
||||
}
|
||||
await prefs.setString('saveDir', selectedDirectory);
|
||||
|
||||
// ignore: use_build_context_synchronously
|
||||
File file = File(
|
||||
'$selectedDirectory/${AppLocalizations.of(context)!.justplace}-${standortC.text}.txt');
|
||||
'$selectedDirectory/${mounted ? AppLocalizations.of(context)!.justplace : const Text('')}-${standortC.text}.txt');
|
||||
|
||||
try {
|
||||
await file.writeAsString(jsonPlace);
|
||||
} catch (e) {
|
||||
// ignore: use_build_context_synchronously
|
||||
if (mounted) {
|
||||
Navigator.pop(context);
|
||||
// ignore: use_build_context_synchronously
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
// ignore: use_build_context_synchronously
|
||||
content: Text(AppLocalizations.of(context)!.savefilefailed)));
|
||||
content: Text(AppLocalizations.of(context)!.savefilefailed))); }
|
||||
return;
|
||||
}
|
||||
// ignore: use_build_context_synchronously
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Datei gespeichert in $selectedDirectory')));
|
||||
}
|
||||
|
||||
Navigator.pushNamedAndRemoveUntil(
|
||||
// ignore: use_build_context_synchronously
|
||||
if (mounted) {
|
||||
Navigator.pushNamedAndRemoveUntil(
|
||||
context,
|
||||
'/home',
|
||||
(route) => false);
|
||||
}
|
||||
}
|
||||
|
||||
// checks if required fields are not empty. If one is the name will be returned
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:geocoding/geocoding.dart';
|
||||
@@ -198,7 +197,7 @@ class KarteState extends State<Karte> {
|
||||
List<Placemark> placemarks = await placemarkFromCoordinates(
|
||||
selectedPosition!.latitude,
|
||||
selectedPosition!.longitude);
|
||||
print(placemarks);
|
||||
// print(placemarks);
|
||||
|
||||
if (selectedPosition != null) {
|
||||
setState(() {
|
||||
|
||||
@@ -32,11 +32,11 @@ class Methods {
|
||||
// ignore: unused_catch_clause
|
||||
} on DioException catch (e) {
|
||||
_errorCode = response.statusCode;
|
||||
print('is hier. var: ${_errorCode}');
|
||||
print('is hier. var: $_errorCode');
|
||||
return;
|
||||
}
|
||||
_errorCode = response.statusCode;
|
||||
print('is hier 2. var: ${_errorCode}');
|
||||
print('is hier 2. var: $_errorCode');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user