Home > Blockchain >  Show button if Future returns true
Show button if Future returns true

Time:09-12

I'm having trouble on an app made in dart, basically I want to show an IconButton if there are any app updates in the site, these requests to the site are asynchronous. The problem is, the function that checks for updates, returns an asynchronous value, which is not accepted by the if, I would like to know if there is any way to convert this value.

I was checking the status code before the app version (for testing), but it does not show "test" on my terminal and neither the variable's value does not change.



    Future updateAvailable() async {
        var response = await http.get(
            Uri.https("...", "..."),
        );
        return response.statusCode == 200; // test
    }
    
    bool canUpdate = false;
    
    Future getCanUpdate() async {
        canUpdate = await updateAvailable();
        debugPrint("test");
    }



    class MyHomePage extends StatelessWidget {
        const MyHomePage({super.key});
    
        getCanUpdate() {}
    
        @override
        Widget build(BuildContext context) {
            return Scaffold(
            appBar: AppBar(
                actions: [
                    if (canUpdate)
                        IconButton(
                            onPressed: () {},
                            icon: const Icon(Icons.download),
                        ),
    //...

I saw this technique from a comment here and I have also tried the accepted answer, but I received this error:


'then' must have a method body because 'MyHomePage' isn't abstract.
Try making 'MyHomePage' abstract, or adding a body to 'then'.

Thanks in advance.

CodePudding user response:

First change your getCanUpdate() to this:

Future<bool> getCanUpdate() async {
    debugPrint("test");
    return await updateAvailable();
  }

then use FutureBuilder like this:

FutureBuilder<bool>(
        future: getCanUpdate(),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Text('Loading....');
            default:
              if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              } else {
                if (snapshot.data!) {
                  return IconButton(
                    onPressed: () {},
                    icon: const Icon(Icons.download),
                  );
                } else {
                  return SizedBox();
                }
              }
          }
        },
      )

CodePudding user response:

Change code like

 Future updateAvailable() async {
    var response = await http.get(
        Uri.https("...", "..."),
    );

 if(response.statusCode == 200){
    return true;
  }else{
    return false;
  }  
}

add set State function

 Future getCanUpdate() async {
        canUpdate = await updateAvailable();
        setState((){});
        
    }

change StateLess to StateFull Widget

  class MyHomePage extends StatelessWidget {
        const MyHomePage({super.key});

        getCanUpdate() {}

        @override
        Widget build(BuildContext context) {
            return Scaffold(
            appBar: AppBar(
                actions: [
                    if (canUpdate)
                        IconButton(
                            onPressed: () {},
                            icon: const Icon(Icons.download),
                        ),
   //...
  • Related