I have different required parameters in flutter and I want to make one of them non required (colour). If I leave it without the required key, it give an error. How can I make it? Here is my code:
class DiscoverCardTemplate extends StatelessWidget {
DiscoverCardTemplate({
required this.textTop,
required this.textMiddle,
required this.textBottom,
required this.coverImage,
required this.onTap,
this.colour,
});
final String textTop, textMiddle, textBottom, coverImage;
final Function onTap;
final Color colour;
make it nullable means colour will be either null or colur_value.
class DiscoverCardTemplate extends StatelessWidget {
DiscoverCardTemplate({
required this.textTop,
required this.textMiddle,
required this.textBottom,
required this.coverImage,
required this.onTap,
this.colour,
});
final String textTop, textMiddle, textBottom, coverImage;
final Function onTap;
final Color? colour;
or
add a default value for colour (if you didn't pass colour then it uses default value).
class DiscoverCardTemplate extends StatelessWidget {
DiscoverCardTemplate({
required this.textTop,
required this.textMiddle,
required this.textBottom,
required this.coverImage,
required this.onTap,
this.colour = colour_value,
});
final String textTop, textMiddle, textBottom, coverImage;
final Function onTap;
final Color colour;
Either make the
final Color colour;
as nullable:
final Color? colour;
Or, assign a default color in the constructor
this.colour = Colors.black,
Related
please help me solve the following problem.
This part of my code:
class MeditationCard extends StatelessWidget {
const MeditationCard({
required this.title,
required this.image,
this.route});
final String title;
final String image;
final String route;
...
I need the route variable as optional, but when I remove the flag, I get an error and ask me to make the variable mandatory.
Tried different approaches but didn't work for me
This alert dialog
The parameter 'route' can't have a value of 'null' because of its
type, but the implicit default value is 'null'.
This happened because of null safety check.In order to set nullable variable, you should use ?, try this:
final String? route;
You can make it nullable (?) or define default value
Example for nullable
class MeditationCard extends StatelessWidget {
const MeditationCard({
required this.title,
required this.image,
this.route});
final String title;
final String image;
final String? route;
I know you got the proper answer to your question. But I wanted to mention another method to solve this issue that might help you in the future, which is to provide a default value. This way you avoid weird null errors and null checking.
class MeditationCard extends StatelessWidget {
const MeditationCard({
required this.title,
required this.image,
required this.route});
final String title;
final String image;
final String route = 'default route';
}
This way you ensure that the property 'route' has a value and is never null. And you can override it when you need to.
I have a class where my object are, I tried calling it out in another file where I want to make use of the properties, It is telling me required named parameter 'image' must be provided.
class _homeState extends State<home> {
var _currentIndex = 0;
final screens = [
HomePage(),
Categories(),
const NearYou(image: '', title: '', subtitle: '',),
Cart(),
Profile()
];
I really don't know what to put in those parameters above
Also, It is saying: Found this candidate, but the arguments don't match.
Will I also call my class in that file?
class NearYou extends StatefulWidget {
final String image;
final String title;
final String subtitle;
const NearYou(
{Key? key,
required this.image,
required this.title,
required this.subtitle})
: super(key: key);
so im working on company project and realize the widget doesn't need Key? and required costructor, and when i try to make it like that it occur red underline.
my project i want to remove the required and '?'
example of what i want
It should view like that:
class CustomButton extends StatelessWidget {
final double? height;
final double width;
final String title;
final double margin;
final Function() onPressed;
const CustomButton ({
Key? key,
this.height,
required this.width,
required this.title,
required this.margin,
required this.onPressed,
}) : super (key: key);
}
If you want to preserve the non-nullable state of your class, you have to pass an initial value for the CustomButton height property.
class CustomButton extends StatelessWidget {
final double height;
final double width;
final String title;
final double margin;
final Function() onPressed;
const CustomButton ({
Key? key,
this.height = 100,
required this.width,
required this.title,
required this.margin,
required this.onPressed,
}) : super (key: key);
}
you have probably defined height as final and that's why it is asking it.
define height as
double? height
So I have a widget that can accept a color or a gradient property :
class OuterWheel extends StatelessWidget {
final double outerRadius;
final double innerRadius;
final TextStyle textStyle;
final double percentage;
final int decimals;
final Color? color;
final List<Color>? gradient;
const OuterWheel({
Key? key,
required this.outerRadius,
required this.innerRadius,
required this.textStyle,
required this.percentage,
required this.decimals,
this.color,
this.gradient,
}) : super(key: key);
I would like to make it like in TypeScript where you could have a union type like Color|List<Color> , so I could only use one property, but apparently we don't have that in Dart.
So how can I make so that you have to choose one of these two fields?
What about mutual exclusive operator?
const OuterWheel({
Key? key,
required this.outerRadius,
required this.innerRadius,
required this.textStyle,
required this.percentage,
required this.decimals,
this.color,
this.gradient,
}) : assert((color == null) ^ (gradient == null), 'color and gradient are mutually exclusive'), super(key: key);
This way you ensure that only one of them, but not both, is not null.
This got a bit tricky for me. since we can no longer pass null values in contructor I had this
class LeadingButton extends StatelessWidget {
final IconData icon;
final Color color;
final Color iconColor;
final double iconSize;
final double size;
final Color backGroundColor;
final VoidCallback onPressed;
final double padding;
final EdgeInsets margin;
final double rotate;
LeadingButton({
#required this.icon,
this.color = Colors.transparent,
#required this.onPressed,
this.size = 20,
this.backGroundColor = Colors.transparent,
this.iconSize = 20,
this.iconColor = darkColor,
this.rotate = 0,
this.padding = 0,
this.margin = const EdgeInsets.all(10)});
...
I am getting an error on icon and onpressed function. although the
documentation is saying
The parameter 'onPressed' & 'icon' can't have a value
of 'null' because of its type, but the implicit default value is
'null'. Try adding either an explicit non-'null' default value or the
'required' modifier.
I guess I am missing some thing, kindly mind to share
Not sure, but it could be that this is due to the recent change of the keyword.
Did you try the required keyword instead?
https://dart.dev/null-safety/faq#how-does-required-compare-to-the-new-required-keyword
I think that LeadingButton's onPressed type needs to be changed to VoidCallback? so it can accept null values.