jancooth
jancooth

Reputation: 567

flutter - Future<bool> convert to bool

I have a future bool method and i want to use this method IconButton color. If i use below codes, i see a error message on my device screen type'Future' is not a subtype of type 'bool' in type of cast.

Future<bool> ifExistInFavoriteList(String url) async {

bool ifExists = false;

SharedPreferences prefs = await SharedPreferences.getInstance(); List my = (prefs.getStringList('myFavoriteList') ?? List());

my.contains(url) ? ifExists = true : ifExists = false;

return ifExists;

}

  bool _isLiked() {
bool a = false;
a = ifExistInFavoriteList(widget.imageUrl) as bool;

return a;

} }

Expanded(
                      child: IconButton(
                        color:
                            _isLiked() ? Colors.deepPurple : Colors.green, 
                        icon: Icon(Icons.category),
                        onPressed: () {
                          //TO-DO
                        },
                      ),
                    )

Upvotes: 8

Views: 9851

Answers (3)

CopsOnRoad
CopsOnRoad

Reputation: 267664

A general answer.

Say this is your function which returns Future<bool>.

Future<bool> myFunc() async => true;

To get the bool value from it,

  1. Use async-await

    void main() async {
      var value = await myFunc(); // value = true
    }
    
  2. Use then:

    void main() {
      bool? value;
      myFunc().then((result) => value = result);
    }
    

Upvotes: 6

Midhun MP
Midhun MP

Reputation: 107141

You can't just simply typecast a Future to bool. Either you need to use await or then syntax to get the bool value from that future. But I suggest you to use a FutureBuilder, which will be the best solution.

FutureBuilder(future: ifExistInFavoriteList(widget.imageUrl),
              builder:(context, snapshot) {
    Color iconColor = Colors.green;
    if (snapshot.hasData && snapshot.data) {
            iconColor = Colors.purple;
    }
    return IconButton(color: iconColor,
                      icon: Icon(Icons.category),
                      onPressed: () {
                         //TO-DO
                      },
           );
     },
),

Upvotes: 3

majd jlassi
majd jlassi

Reputation: 51

Yes because ifExistInFavoriteList(String url) is of type Future<bool>, you need to use FutureBuilder widget to get the bool value.

Expanded(
      child: FutureBuilder(
          future: ifExistInFavoriteList(widget.imageUrl),
          builder: (context, asyncSnapshot){
            if(asyncSnapshot.hasData){
              final _isLiked = asyncSnapshot.data;
              return IconButton(
                color:
                _isLiked() ? Colors.deepPurple : Colors.green,
                icon: Icon(Icons.category),
                onPressed: () {
                  //TO-DO
                },
              );
            }
          return IconButton(
            color:Colors.grey,
            icon: Icon(Icons.category),
            onPressed: () {
              //TO-DO
            },
          );
      },),
    ),

Upvotes: 3

Related Questions