Search code examples
flutterflutter-layoutproviderflutter-provider

'Widget?' can't be assigned to the parameter type 'Widget'


when i assign consumer child:ch that time error shown this:- The argument type 'Widget?' can't be assigned to the parameter type 'Widget'

The code is :

...
Consumer<Cart>(
            builder: (_, cart, ch) => Badge(
                child: ch,
                value: cart.iteamCount.toString(),
                color: Theme.of(context).accentColor),
            child: IconButton(
              icon: Icon(
                Icons.shopping_cart,
              ),
              onPressed: () {},
            ),
          ),
...

Solution

  • It happens because ch is a Widget which is nullable i.e it's defined as Widget?.

    So to avoid this , one work around is add not null operator !

    Change

    child: ch
    

    to

    child: ch!