Q: "Unhandled Exception: setState() or markNeedsBuild() called during build" when calling Flutter's showDialog() I encountered with the error described in the title, and later found the culprit, which was a call of showDialog() inside a StreamBuilder.
TL;DR Solution
If calling showDialog()inside StreamBuilder, deleting it might solve the problem.
The code is sampled below:

class SomeClass extends StatefulWidget {
  final String restaurantId;

  @override
  _HomePageState createState() => _HomePageState();
}

class _SomeClassState extends State<SomeClass> {

 ...
 @override
 Widget build(BuildContext context) {
    ...

    return StreamBuilder<T>(
        stream: _stream,
        builder: (context, snapshot) {

          // Wait for connection to stablish.
          if (snapshot.connectionState == ConnectionState.waiting)
            Dialogs.showLoadingDialog(context, _keyLoader);

          // Validate there's snapshot data; else, return error.
          if (snapshot.hasData) {
             ... *Build some Scaffold*
          } else {
            ... *Build some Error Message*
          }
        }
    );
 } 


class Dialogs {
  static Future<void> showLoadingDialog(
      BuildContext context, GlobalKey key) async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return new WillPopScope(
          onWillPop: () async => false,
          child: Container(
            key: key,
            child: ...
          ),
        );
      },
    );
  }
}


Why is that, and how can I be able to call showDialog() properly? I found that by deleting the call to showDialog() inside Dialogs.showLoadingDialog() completely solved the problem.
This is the complete error output:
Unhandled Exception: setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
  Overlay-[LabeledGlobalKey<OverlayState>#c2441]
The widget which was currently being built when the offending call was made was:
  StreamBuilder<Restaurant>
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4292:11)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4307:6)
#2      State.setState (packag<…>
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2416 pos 18: '!navigator._debugLocked': is not true.

...


A: You need to wait for the widget to build and then call showDialog.
WidgetsBinding.instance.addPostFrameCallback((_) {
    // show dialog here
}


A: I think you need to return a widget from your builder function.
 Dialogs.showLoadingDialog(context, _keyLoader);
you need to add 'return' befor this.

A: Before UI Build we can not call setState. WidgetsBinding.instance.addPostFrameCallback is method which give call back when build is complted.
