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