I am new to flutter, I am receiving this warning over a function that I called in InitState , the main aim of the function was to run and ask the permission to access location of the user. Could anyone please let me know why m I getting this warning and is there a better way to write the following code. Any help would be appreciated.
class _UserLocationState extends State<UserLocation> {
void initState() {
super.initState();
permissionGranted();
}
Location location = new Location();
PermissionStatus? _allowed;
@override
Future<void> permissionGranted() async {
final PermissionStatus permissionRequested;
_allowed = await location.hasPermission();
if (_allowed == PermissionStatus.denied) {
print('Permission has been denied');
permissionRequested = await location.requestPermission();
setState(() {
_allowed = permissionRequested;
});
} else
setState(() {
_allowed = PermissionStatus.granted;
});
}
//final _userLocation = await location.getLocation();
@override
Widget build(BuildContext context) {
return Container();
}
}
CodePudding user response:
You have used @override just above the 'permissionGranted()' method due to which you are getting 'The method doesn't override an inherited method' as 'permissionGranted()' is user defined one. You should use @override above 'initstate()'. Or, just copy it -
class _UserLocationState extends State<UserLocation> {
@override
void initState() {
super.initState();
permissionGranted();
}
Location location = new Location();
PermissionStatus? _allowed;
Future<void> permissionGranted() async {
final PermissionStatus permissionRequested;
_allowed = await location.hasPermission();
if (_allowed == PermissionStatus.denied) {
print('Permission has been denied');
permissionRequested = await location.requestPermission();
setState(() {
_allowed = permissionRequested;
});
} else
setState(() {
_allowed = PermissionStatus.granted;
});
}
//final _userLocation = await location.getLocation();
@override
Widget build(BuildContext context) {
return Container();
}
}
CodePudding user response:
initState
should be used as an overridden method. So, you're missing @override
.
@override //add this
void initState() {
super.initState();
permissionGranted();
}