提问者:小点点

不能从返回类型为“Widget”的函数返回值“null”,因为“Widget”不可为空


我错过了什么?是因为飞镖版本还是其他东西,

environment:
  sdk: ">=2.16.1 <3.0.0"

1.错误

错误:无法从返回类型为“Widget”的函数返回值“null”,因为“Widget”不可为空。

  • 'Widget'来自'pack: flutter/src/widget/frame.dart'

//显示警报对话框

showAlertDialog(BuildContext context) {
    AlertDialog alert = AlertDialog(
      content: Padding(
        padding: const EdgeInsets.only(top: 22.0, bottom: 22),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              child: Text(
                "Would you like to get latest updates and notifications?",
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.black, fontSize: 18),
              ),
            ),
            SizedBox(height: 24),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
              FlatButton(
                onPressed: () {
                  setState(() {
                    snackBarText = "You will not recive notifications.";
                  });
                  Navigator.of(context).pop();
                  Navigator.of(context).pushReplacement(new MaterialPageRoute(
                      builder: (BuildContext context) => null));
                },
                child: Text(
                  "DENY",
                  style:
                      TextStyle(color: Colors.white, fontSize: 16, height: 1.2),
                ),
                color: Colors.purple,
              ),
              SizedBox(width: 10),
              FlatButton(
                onPressed: () {
                  setState(() {
                    snackBarText = "You will recive notifications.";
                  });
                  Navigator.of(context).pop();
                  Navigator.of(context).pushReplacement(new MaterialPageRoute(
                      builder: (BuildContext context) => null));
                },
                child: Text(
                  "ALLOW",
                  style: TextStyle(
                      color: Colors.purple, fontSize: 16, height: 1.2),
                ),
                shape: RoundedRectangleBorder(
                    side: BorderSide(width: 1, color: Colors.purple)),
                color: Colors.white,
              )
            ])
          ],
        ),
      ),
    );
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

**2.**错误

lib/ua_Screens. dart:29:8:错误:字段'_image'应该被初始化,因为它的类型'File'不允许null。

  • 文件来自dart: io。文件_image; ^^^^^^
File _image;
Future get_image() async {
    final image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _image = image;
    });
  }

共1个答案

匿名用户

我认为你写错了代码,试试下面的代码希望对使用有帮助。还有一件事不要使用FlatButton使用TextButton,因为FlatButton是由flutter depriciated的。

参考TextButton

您的警报对话框功能:

showAlertDialog(BuildContext context) {
    return AlertDialog(
      content: Padding(
        padding: const EdgeInsets.only(top: 22.0, bottom: 22),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              child: Text(
                "Would you like to get latest updates and notifications?",
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.black, fontSize: 18),
              ),
            ),
            SizedBox(height: 24),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
              FlatButton(
                onPressed: () {
                  setState(() {});
                  Navigator.of(context).pop();
                  Navigator.of(context).pushReplacement(new MaterialPageRoute(
                      builder: (BuildContext context) => null));
                },
                child: Text(
                  "DENY",
                  style:
                      TextStyle(color: Colors.white, fontSize: 16, height: 1.2),
                ),
                color: Colors.purple,
              ),
              SizedBox(width: 10),
              FlatButton(
                onPressed: () {
                  setState(() {});
                  Navigator.of(context).pop();
                  Navigator.of(context).pushReplacement(new MaterialPageRoute(
                      builder: (BuildContext context) => null));
                },
                child: Text(
                  "ALLOW",
                  style: TextStyle(
                      color: Colors.purple, fontSize: 16, height: 1.2),
                ),
                shape: RoundedRectangleBorder(
                    side: BorderSide(width: 1, color: Colors.purple)),
                color: Colors.white,
              )
            ])
          ],
        ),
      ),
    );
  }

您的小部件:

ElevatedButton(
            onPressed: () {
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return showAlertDialog(context);
                },
              );
            },
            child: Text('Pressed Me'),
          ),

你的第二个错误参考我的答案这里和这里