Search code examples
flutterflutter-provider

The argument type 'bool' can't be assigned to the parameter type 'Widget'


how do i fix this error? I want to call the return of my provider. in this way but it returns me the error mentioned in the title.

IconButton(
   icon: Heart.isActive,
    onPressed: () {
     setState(() {
      Heart.changeState();
         });
                            },

try to remove the datatype from the getter, but it throws errors in the model.

get isActive => _isActive;

here the complete provider

class HeartIconState with ChangeNotifier {
  bool _isActive = false;

  bool get isActive => _isActive;

  set isActive(bool value) {
    _isActive = value;

    notifyListeners();
  }

  void changeState() {
    isActive //inicia en false
        ? const Icon(
            Icons.favorite,
            color: Colors.red,
          )
        : const Icon(
            Icons.favorite_border,
            color: Colors.red,
          );

    notifyListeners();
  }

                            

Solution

  • Thats obviously clear that your passing bool which named as isActive Instead of passing that use your function

    Icon isActiveIcon() {
        return isActive
        ? const Icon( 
            Icons.favorite, 
            color: Colors.red) 
        : const Icon( 
            Icons.favorite_border, 
            color: Colors.red);
    }
    

    Now use it instead of your Icon

    icon: Heart.isActiveIcon(),
    

    You need to refactor changeState too

    void changeState() { 
        isActive = !isActive; 
        notifyListeners(); 
    }