I think I have a simple question but I don’t know why its not work
Simple:
main.dart
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const Welcome(),
);
}
}
Second file:
welcome.dart
class Welcome extends StatelessWidget {
const Welcome({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
appBar: AppBarNav(
title: 'JobFinder',
),
);
}
}
AppBar file:
appbar.dart
import 'package:flutter/material.dart';
class AppBarNav extends StatelessWidget {
final String title;
const AppBarNav({super.key, required this.title});
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(title),
);
}
}
Debbuger say:
The argument type ‘AppBarNav’ can’t be assigned to the parameter type ‘PreferredSizeWidget?’
Why?
Do you know why the debugger tells me that I can’t use AppBar in the Welcome.dart file?
I know it’s probably simple but I can’t find a solution to it.
Generally I want to place an appBar on every screen (same Appbar)
2
Answers
the argument
appBar
needs to be directly anAppBar
. You are giving it anAppBarNav
which is not anAppBar
even if it only consists of one.Technically it doesn’t need to be an
AppBar
but aPreferredSizeWidget
which anAppBar
implements. So alternatively you could also let yourAppBarNav
implementPreferredSizeWidget
like thisBut then you need to provide an implementation for
preferredSize
alsoSimple solution
Or maybe this one :
You can check this answer and this documentation example.