值“null”;无法从返回类型为“Widget”的函数返回因为“小部件”不可为空

发布于 2025-01-10 23:43:34 字数 2964 浏览 0 评论 0原文

我错过了什么?是因为dart版本还是其他原因,

environment:
  sdk: ">=2.16.1 <3.0.0"

1.错误

错误:无法从带有 return 的函数返回值“null” 输入“Widget”,因为“Widget”不可为空。

  • “Widget”来自“package:flutter/src/widgets/framework.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”。文件_图像; ^^^^^^
File _image;
Future get_image() async {
    final image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _image = image;
    });
  }

what'd I miss ? is it because dart version or other things ,

environment:
  sdk: ">=2.16.1 <3.0.0"

1. error

Error: The value 'null' can't be returned from a function with return
type 'Widget' because 'Widget' is not nullable.

  • 'Widget' is from 'package:flutter/src/widgets/framework.dart'

// Alertdialog box showing

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.**error

lib/ua_Screens.dart:29:8: Error: Field '_image' should be initialized
because its type 'File' doesn't allow null.

  • 'File' is from 'dart:io'. File _image;
    ^^^^^^
File _image;
Future get_image() async {
    final image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _image = image;
    });
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2025-01-17 23:43:34

我认为你写了错误的代码,尝试下面的代码希望它对使用有所帮助。还有一件事不使用 FlatButton 使用 TextButton 因为 FlatButton 因抖动而贬值。

请参考 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'),
          ),

您的结果屏幕 -> 输入图片此处描述

您的警报对话框 -> 输入图片此处描述

您的第二个错误请参考我的答案此处此处

I think you write wrong code, try below code hope its help to used. and one thing dont used FlatButton used TextButton because FlatButton is depriciated by flutter.

Refer TextButton

Your Alert Dialog function:

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,
              )
            ])
          ],
        ),
      ),
    );
  }

Your Widget:

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

Your result screen-> enter image description here

Your alert dialog-> enter image description here

Your Second error refer my answer here and here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文