Didier Supernover
Didier Supernover

Reputation: 617

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

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

Upvotes: 0

Views: 1345

Answers (1)

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14885

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

Upvotes: 1

Related Questions