参数 'text'、'onPressed'、'outlineBtn' 由于其类型不能具有 'null' 的值,但隐式默认值为 'null'
The parameter 'text', 'onPressed', 'outlineBtn' can't have a value of 'null' because of its type, but the implicit default value is 'null'
代码显示的错误是所有参数 text、onPressed、outlineBtn 都不能具有 null 值,当我在它们之前使用 require 关键字时这个问题得到解决,但随后它开始显示此错误.
错误:
运行Gradle任务'assembleDebug'...
lib/Widget/custom_btn.dart:12:24:警告:空感知操作的操作数'??'具有 'bool' 类型,不包括 null。
bool _outlineBtn = outlineBtn ??错误的;
^
lib/Widget/custom_btn.dart:33:11:警告:空感知操作的操作数'??'具有类型 'String',不包括 null。
文本 ?? "文字",
这是我的代码:
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,
),
),
),
);
}
}
发生这种情况是因为 required
关键字表示必须提供变量并且不能为空。在你的情况下 outlineBtn
永远不能为空。
bool _outlineBtn = outlineBtn ?? false;
要解决此问题,您可以省略 null 检查或省略 required
关键字并将变量更改为可为 null。
final String? text;
final Function? onPressed;
final bool? outlineBtn;
代码显示的错误是所有参数 text、onPressed、outlineBtn 都不能具有 null 值,当我在它们之前使用 require 关键字时这个问题得到解决,但随后它开始显示此错误.
错误:
运行Gradle任务'assembleDebug'... lib/Widget/custom_btn.dart:12:24:警告:空感知操作的操作数'??'具有 'bool' 类型,不包括 null。 bool _outlineBtn = outlineBtn ??错误的; ^ lib/Widget/custom_btn.dart:33:11:警告:空感知操作的操作数'??'具有类型 'String',不包括 null。 文本 ?? "文字",
这是我的代码:
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,
),
),
),
);
} }
发生这种情况是因为 required
关键字表示必须提供变量并且不能为空。在你的情况下 outlineBtn
永远不能为空。
bool _outlineBtn = outlineBtn ?? false;
要解决此问题,您可以省略 null 检查或省略 required
关键字并将变量更改为可为 null。
final String? text;
final Function? onPressed;
final bool? outlineBtn;