Home > Mobile >  make function await flutter
make function await flutter

Time:08-16

I have this variable

  dynamic _permission = true;

after clicked on button. I need to display sncakbar if _permission= false thats mean user has no permission to access the page else go to page.

but I faced an issue that _permission always equal true. even if permission return false from api.

here is the code

Future<void> checkPermesssion() async {
    String api_token = await getToken();
    final response = await http.post(
      Uri.parse(PermissionURL),
      headers: {'Accept': 'application/json',
        'Authorization': 'Bearer $api_token'
      },
    );
    var permission = jsonDecode(response.body)['permission'];
    setState(() {
      _permission = permission;
    });
  }

and here is the button code

 child: ElevatedButton(
                  onPressed: () {
                   checkPermesssion();
                    _permission ?
                    Navigator.of(context).push(
                        MaterialPageRoute(builder: (context) => page()))
                        :
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text(
                            'You don\'t have permession to access this page '),
                        behavior: SnackBarBehavior.floating,
                        margin: EdgeInsets.all(50),
                        elevation: 30,
                        action: SnackBarAction(
                          label: 'Dismiss',
                          disabledTextColor: Colors.white,
                          textColor: Colors.yellow,
                          onPressed: () {
                            ScaffoldMessenger.of(context).hideCurrentSnackBar();
                          },
                        ),


                      ),
                    );
                  },

How can I do that please

CodePudding user response:

Simply put await before calling the function

ElevatedButton(
  onPressed: () async {
    await checkPermesssion();
    _permission ?
    Navigator.of(context).push(
        MaterialPageRoute(builder: (context) => page()))
        :
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(
            'You don\'t have permession to access this page '),
        behavior: SnackBarBehavior.floating,
        margin: EdgeInsets.all(50),
        elevation: 30,
        action: SnackBarAction(
          label: 'Dismiss',
          disabledTextColor: Colors.white,
          textColor: Colors.yellow,
          onPressed: () {
            ScaffoldMessenger.of(context).hideCurrentSnackBar();
          },
        ),
      ),
    );
  },
),

CodePudding user response:

Try this for button's code :

child: ElevatedButton(
                  onPressed: () async{
                   await checkPermesssion();
                    _permission ?
                    Navigator.of(context).push(
                        MaterialPageRoute(builder: (context) => page()))
                        :
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text(
                            'You don\'t have permession to access this page '),
                        behavior: SnackBarBehavior.floating,
                        margin: EdgeInsets.all(50),
                        elevation: 30,
                        action: SnackBarAction(
                          label: 'Dismiss',
                          disabledTextColor: Colors.white,
                          textColor: Colors.yellow,
                          onPressed: () {
                            ScaffoldMessenger.of(context).hideCurrentSnackBar();
                          },
                        ),


                      ),
                    );
                  },
  • Related