Search code examples
flutterdartflutter-appbar

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


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,
      );
    }