the argument type 'widget' can't be assegned to the parameter type 'PreferredSizeWidget?'

Issue

the argument type ‘widget’ can’t be assegned to the parameter type ‘PreferredSizeWidget?’

class home_screen extends StatelessWidget {
  const home_screen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: myAppBar(),
    );
  }
}

Widget myAppBar() {
  return AppBar(
    backgroundColor: Colors.red,
    elevation: 0,
  );
}

Solution

An AppBar implements PreferredSizeWidget and Scaffold expect the appBar property to be of type PreferredSizeWidget

Simply do:

PreferredSizeWidget myAppBar() {
  return AppBar(
    backgroundColor: Colors.red,
    elevation: 0,
  );
}

Answered By – Josteve

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *