Reputation: 198
The code is showing the error that all of the parameters that is text, onPressed, outlineBtn can't have the value null and this problem gets solved when I use the require keyword before them but then it starts showing this error.
Error:
Running Gradle task 'assembleDebug'... lib/Widget/custom_btn.dart:12:24: Warning: Operand of null-aware operation '??' has type 'bool' which excludes null. bool _outlineBtn = outlineBtn ?? false; ^ lib/Widget/custom_btn.dart:33:11: Warning: Operand of null-aware operation '??' has type 'String' which excludes null. text ?? "Text",
Here is my code:
import 'package:flutter/material.dart';
class CustomBtn extends StatelessWidget {
final String text;
final Function onPressed;
final bool outlineBtn;
CustomBtn(
{required this.text, required this.onPressed, required this.outlineBtn});
@override
Widget build(BuildContext context) {
bool _outlineBtn = outlineBtn ?? false;
return GestureDetector(
onTap: onPressed(),
child: Container(
height: 60.0,
alignment: Alignment.center,
decoration: BoxDecoration(
color: _outlineBtn ? Colors.transparent : Colors.black,
border: Border.all(
color: Colors.black,
width: 2.0,
),
borderRadius: BorderRadius.circular(12.0),
),
margin: EdgeInsets.symmetric(
horizontal: 24.0,
vertical: 24.0,
),
child: Text(
//Create New Account
text ?? "Text",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w600,
color: _outlineBtn ? Colors.black : Colors.white,
),
),
),
);
} }
Upvotes: 0
Views: 519
Reputation: 3557
This happens because the required
keyword says that the variable must be provided and it cannot be null. In you case outlineBtn
can never be null.
bool _outlineBtn = outlineBtn ?? false;
To fix this you can either omit the null check or omit the required
keyword and change your variable to nullable.
final String? text;
final Function? onPressed;
final bool? outlineBtn;
Upvotes: 1