title: Flutter get result of BuildContext.findRenderObject
body: <p>I am trying to capture a <code>PNG</code> image of a Flutter Widget for use in a <code>MapBox</code> instance. The code requires that I detect when the Build process has completed, and the rendered area is ready to paint. Here is my <code>Widget</code>:</p>
  <pre><code>
  typedef void ImageCreatedCallback(UI.Image image);
  
  class POIWidget extends StatefulWidget {
    const POIWidget({Key key, this.poi, this.imageCreatedCallback})
        : super(key: key);
    final POI poi;
    final ImageCreatedCallback imageCreatedCallback;
  
    @override
    _POIWidgetState createState() =&gt; _POIWidgetState();
  }
  
  class _POIWidgetState extends State&lt;POIWidget&gt; {
    /**
     *  The only purpose of this widget is to provide an Image version of itself
     *  that can then be used in the MapBox map.
     */
    GlobalKey globalKey = GlobalKey();
  
    Future&lt;void&gt; _capturePng() async {
      RenderRepaintBoundary boundary =
          globalKey.currentContext.findRenderObject() as RenderRepaintBoundary;
  
      if (boundary == null) {
        // print(&quot;Waiting for boundary to be painted.&quot;);
        await Future.delayed(const Duration(milliseconds: 100));
        return _capturePng();
      }
      MediaQueryData queryData = MediaQuery.of(context);
      double devicePixelRatio = queryData.devicePixelRatio;
      UI.Image image = await boundary.toImage(pixelRatio: devicePixelRatio);
      widget.imageCreatedCallback(image);
    }
  
    @override
    void initState() {
      super.initState();
      WidgetsBinding.instance.addPostFrameCallback(
        (_) async {
          await Future.delayed(const Duration(milliseconds: 100))
              .then((_) =&gt; _capturePng());
        },
      );
    }
  
    @override
    Widget build(BuildContext context) {
      return RepaintBoundary(
        key: globalKey,
        child: ClipOval(
          child: Material(
            color: widget.poi.iconBackgroundColor,
            child: SizedBox(
              width: widget.poi.iconSize.size * 2,
              height: widget.poi.iconSize.size * 2,
              child: widget.poi.iconName != null
                  ? IconManager.instance.getIconFromName(
                      widget.poi.iconName,
                      size: widget.poi.iconSize.size,
                      color: widget.poi.iconColor,
                    )
                  : IconManager.instance.iconMapPin,
            ),
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>You'll notice that the <code>_capturePNG</code> method has this line:</p>
  <pre><code>RenderRepaintBoundary boundary =
          globalKey.currentContext.findRenderObject() as RenderRepaintBoundary;
  </code></pre>
  <p>and then immediately afterwards I am testing to see if this is <code>null</code>. I think this is being ignored as it is not <code>null</code> but something else. It is, however, not what I need to create the image later on in the method.</p>
  <p>I get this error message from this code:</p>
  <pre><code>[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Null check operator used on a null value
  E/flutter (21697): #0      RenderRepaintBoundary.toImage (package:flutter/src/rendering/proxy_box.dart:3089)
  E/flutter (21697): #1      _POIWidgetState._capturePng (package:screens/map/widgets/POIWidget.dart:38)
  E/flutter (21697): #2      _POIWidgetState.initState.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:screens/map/widgets/POIWidget.dart:48)
  E/flutter (21697): #3      _rootRunUnary (dart:async/zone.dart:1362)
  E/flutter (21697): #4      _CustomZone.runUnary (dart:async/zone.dart:1265)
  E/flutter (21697): &lt;asynchronous suspension&gt;
  E/flutter (21697): #5      _POIWidgetState.initState.&lt;anonymous closure&gt; (package:screens/map/widgets/POIWidget.dart:47)
  E/flutter (21697): &lt;asynchronous suspension&gt;
  </code></pre>
  <p>How can I be sure the widget is built and the <code>RenderRepaintBoundary</code> is ready to be captured?</p>