24

He everyone,

I have an issue with the following code:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Expanded(
          child: PageView(
            children: <Widget>[
              Container(
                color: Colors.red,
              ),
              Container(
                color: Colors.yellow,
              ),
              Container(
                color: Colors.green,
              ),
              Container(
                color: Colors.blue,
              ),
            ],
          ),
        ),
      ],
    );
  }
}

When I run it using "flutter run" it displays exactly what I need but when I use the "--release" parameter it completely stops working and displays a grey screen. Any help is appreciated!

5 Answers 5

55

You're using Expanded inside a Widget (Stack) who has its own fit. In order to fix it, remove Expanded and apply the fit parameter to your Stack

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<ThisApp> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      fit: StackFit.expand, // StackFit.expand fixes the issue
      children: <Widget>[
        PageView(
          children: <Widget>[
            Container(
              color: Colors.red,
            ),
            Container(
              color: Colors.yellow,
            ),
            Container(
              color: Colors.green,
            ),
            Container(
              color: Colors.blue,
            ),
          ],
        )
      ],
    );
  }
}

Using debug mode, you'd notice the stack trace telling your about that error. Because --release always try to avoid issues/crashes, will disable that part of the UI, aka = grey screen.

4
  • 1
    Thank you so much for this!!
    – maplesyrup
    Commented Feb 19, 2020 at 4:52
  • 1
    you are awesome Commented Aug 25, 2020 at 10:42
  • I had the problem of the grey screen on a Huawei phone (Android 10), but not on my tablet (Android 5), or my Samsung note 9 (Android 10), or even on the emulator. All I did was remove the Expanded and it all worked again. Thank you.
    – David
    Commented Jun 3, 2021 at 18:03
  • Bruh, you saved me, i should have focused on stack trace. Commented May 11, 2022 at 11:53
8

In my case, there were no errors or warnings in debug mode. I fixed the grey screen by running the app in the release mode.

Using Android Studio,

  • Connect a physical device
  • Click on Run > Flutter Run 'main.dart' in Release Mode

Running flutter app in release mode

It will also save the apk at Built build\app\outputs\flutter-apk\app-release.apk

1
  • Also, in Run->Edit Configurations, add Build Flavor name Commented Mar 18, 2023 at 14:39
3

The grey screen shows up in release instead of the red error message that shows up in debug builds because Flutter decided it was better than showing an error message to the user. In my case, one of my users was reporting the issue and I could not reproduce it. Turns out, if you have Crashlytics enabled, the errors show up as non fatals. This made it simple to find the problem.

0

I have used de debug to discover what was wrong and it was pretty verbose and precise on the position of the error (in my case it was the Positioned inside a Center widget).

1
  • 5
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 15, 2021 at 23:53
0

For me it was because I have placed Positioned widget inside Column widget while it should be inside Stack widget.
I have only replaced Positioned with Container and removed the attributes (right, left, top) and the app worked perfectly in iOS release mode.

Not the answer you're looking for? Browse other questions tagged or ask your own question.