i dont know why i am getting the errors,i have previously coded the same and it worked perfectly fine maybe some new update or plugin please help me solve my error
import 'package:flutter/material.dart';
class RoundedButton extends StatelessWidget {
final String title;
final Color colour;
final Function onPressed;
RoundedButton({this.title, this.colour, @required this.onPressed});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Material(
elevation: 5.0,
color: colour,
borderRadius: BorderRadius.circular(30.0),
child: MaterialButton(
onPressed: onPressed,
minWidth: 200.0,
height: 42.0,
child: Text(
title,
style: TextStyle(color: Colors.white),
),
),
),
);
}
}
You've updated your Dart SDK to a version which supports null-safety. With null safety there are some differences to the way constructors work in Dart.
@required
is now simply required
- No @ symbol anymore.String?
or Color?
.Using default values, your code might look something like this:
final String title;
final Color colour;
final void Function() onPressed;
RoundedButton({
this.title = "",
this.colour = Colors.blue,
required this.onPressed,
});
The rest of your class would be the same.
On the other hand, if you make the properties nullable, you might end up with something like this:
final String? title;
final Color? colour;
final void Function() onPressed;
RoundedButton({
this.title,
this.colour,
required this.onPressed,
});
Now, for the colour
property, you won't need to change anything, since Material.color
can be null. However, since Text
requires a non-nullable String
, you'll have to change the way you pass the title
property:
Text(
title ?? "", // If title is null, use an empty string
// ...
)
or
child: title == null // Only build the Text widget if title is not null
? null
: Text(
title,
// ...
)
In depth reading on null safety here: https://dart.dev/null-safety/understanding-null-safety