'Flutter - A non-null value must be returned since the return type 'Widget' doesn't allow null

I tried to run this code but it gives me the error provided in the title, also it says (The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type.), any help to solve this issue would be appreciated!

    Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection("chats").snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
        if(snapshot.hasError) {
          return Center (
            child: Text("There is a problem"),
          );
        }

        if(snapshot.connectionState == ConnectionState.waiting) {
          return Center (
            child: Text("Loading"),
          );
        }

        if(snapshot.hasData) {
          return CustomScrollView(
            slivers: [
              CupertinoSliverNavigationBar(
                largeTitle: Text("Chats"),
              ),

              SliverList(
                delegate: (
                    SliverChildListDelegate(
                      snapshot.data!.docs.map((DocumentSnapshot document) {
                        Map<String, dynamic> data = document.data()!as Map<String, dynamic>;
                        return CupertinoListTile(title: Text(data['title']),
                        );
                      }).toList())))
            ],
          );
      }
  });
  }


Solution 1:[1]

From the builder you are retuning widget with checking conditions, and it is good practice. You can return a default widget at the end. Also, you can use else state. The error show up by editor because it thinks we might face other state and that might not fit with theses if. It expects a default return widget that be with or without else condition.

builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
 
if(...)

if(...)


else return Text(...); // with or without else condition it will retrun by default. 
}

Solution 2:[2]

def predict_age(input_path: str):
"""Predict the age of the faces showing in the image"""
# Read Input Image
img = cv2.imread(input_path)
# Take a copy of the initial image and resize it
frame = img.copy()

Seems your error happens here because img is None, so it has no method copy() to call.

You said you are running the code like this:

 .\predict_age.py /tmp

I can see that the code initialises img with the input_path which is passed as sys.argv[1]. well, /tmp is not really an image, could you try passing an image like .\predict_age.py /tmp/my_image.png

Solution 3:[3]

The error means that the img variable from cv2.imread(input_path) is None. I.e., something went wrong with reading the image from input_path.

In your main code, you write

import sys
image_path = sys.argv[1]
predict_age(image_path)

So the image path is given by the first argument to the program. Are you running the code as python predict_age.py 3-people.jpg?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yeasin Sheikh
Solution 2 MiTriPy
Solution 3