if statement on OnPressed flutter

Issue

permission_handler: ^8.3.0

I inserted permission handler in pubspec.yaml

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.MANAGE_ONGOING_CALLS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

and here are permissions that users need to run the App.

I have one raisedbutton and I wish to ask permission on a first click and on second click, show alert diaglog saying "thank you".

here is what I got so far

getPermission() async {
  var status = await Permission.contacts.status;
  if (status.isGranted) {
    print('Approved');
  } else if (status.isDenied) {
    print('Denied');
    Permission.contacts.request();
  }
}

 ElevatedButton(onPressed: () {
    if(condition == true){
       getPermission();
     }else{
       showDialog(context: context,
       builder: BuildContext()
       title: column(
       content: Text('Thank You'),
    );
  );
}

It seems like if statement after elevatedbutton is the problem but I can’t exactly pin point the problem to fix. Thanks in advance

Solution

RaisedButton(onPressed: () async {
    if(await Permission.contacts.status == status.isDenied){
      getPermission();
     }else{
       showDialog(context: context,
       builder: BuildContext()
       title: column(
       content: Text('Thank You'),
    );
  );}

so?

Answered By – Maurice Raguse

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *