'GridView inside Expanded not visible, only visible when using Container with a specified height

I have a gridview that I'd like to display in a non-specific height container. However, the gridview only shows up when I use container with a specified height. GridView goes invisible when I change Container to Expanded or Flexible.

Working Code:

return Container( //using container
  height: 250,    //with a specific height
  child: GridView.count(
    primary: false,
    padding: const EdgeInsets.all(0.0),
    crossAxisSpacing: 10.0,
    crossAxisCount: 2,
    children: <Widget>[
      const Text('He\'d have you all unravel at the'),
      const Text('Heed not the rabble'),
      const Text('Sound of screams but the'),
      const Text('Who scream'),
    ],
  ),
);

Non-working code:

return Expanded(
  child: GridView.count(
    primary: false,
    padding: const EdgeInsets.all(0.0),
    crossAxisSpacing: 10.0,
    crossAxisCount: 2,
    children: <Widget>[
      const Text('He\'d have you all unravel at the'),
      const Text('Heed not the rabble'),
      const Text('Sound of screams but the'),
      const Text('Who scream'),
    ],
  ),
);


Solution 1:[1]

You have to expand into a Column Widget, it will takes all the available vertical space

return Column(
    children:<Widget>[
      Expanded(
        child: GridView.count(
          primary: false,
          padding: const EdgeInsets.all(0.0),
          crossAxisSpacing: 10.0,
          crossAxisCount: 2,
          children: <Widget>[
            const Text('He\'d have you all unravel at the'),
            const Text('Heed not the rabble'),
            const Text('Sound of screams but the'),
            const Text('Who scream'),
          ],
        ),
      )
    ]
);

Solution 2:[2]

The parent widget of gridview need to have a certain height. height: MediaQuery.of(context).size.height is one of the option if the container is not scrollable or similar answer has already been posted please refer the link.

Solution 3:[3]

set shrinkWrap: true inside GridView.

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 Tom Rivoire
Solution 2 Harshal Bhope
Solution 3 Harshit Pathak