Repository: flutter
Issue: Unhandled exception when calling first time RenderRepaintBoundary.toImage()
Labels: c: crash,framework,engine,c: rendering,customer: octopod,has reproducible steps,P2,found in release: 3.3,found in release: 3.6,team-engine,triaged-engine

**flutter doctor -v**
```
[√] Flutter (Channel beta, v0.8.2, on Microsoft Windows [Version 10.0.17134.285], locale de-DE)
    • Flutter version 0.8.2 at C:\flutter
    • Framework revision 5ab9e70727 (3 weeks ago), 2018-09-07 12:33:05 -0700
    • Engine revision 58a1894a1c
    • Dart version 2.1.0-dev.3.1.flutter-760a9690c2

[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at C:\Users\marko\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 2.3)
    • Android Studio at C:\Program Files\Android\Android Studio_2_3_3
    • Flutter plugin version 23.2.2
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b06)

[√] Android Studio (version 3.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 28.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[√] VS Code, 64-bit edition (version 1.24.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 2.14.0

[√] Connected devices (1 available)
    • FIG LX1 • XEDDU18122003040 • android-arm64 • Android 8.0.0 (API 26)

• No issues found!
```

**Error message log from Console:**
```
E/flutter (25726): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (25726): 'package:flutter/src/rendering/proxy_box.dart': Failed assertion: line 2623 pos 12: '!debugNeedsPaint': is not true.
E/flutter (25726): #0      _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39)
E/flutter (25726): #1      _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5)
E/flutter (25726): #2      RenderRepaintBoundary.toImage (package:flutter/src/rendering/proxy_box.dart:2623:12)
E/flutter (25726): #3      _PngHomeState._capturePng (file:///C:/Workspaces/AS/flutter/screenshot_a_widget/screenshoting_a_widget/lib/main.dart:49:37)
E/flutter (25726): <asynchronous suspension>
E/flutter (25726): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:503:14)
E/flutter (25726): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:558:30)
E/flutter (25726): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (25726): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (25726): #8      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
E/flutter (25726): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter (25726): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (25726): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (25726): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter (25726): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (25726): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (25726): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (25726): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (25726): #17     _invoke1 (dart:ui/hooks.dart:142:13)
E/flutter (25726): #18     _dispatchPointerDataPacket (dart:ui/hooks.dart:99:5)
```
**Source code** of the program (mainly taken from the docs of proxy_box.dart  in class RenderRepaintBoundary method: toImage):
```dart
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new PngHome(),
    );
  }
}

class PngHome extends StatefulWidget {
  PngHome({Key key}) : super(key: key);

  @override
  _PngHomeState createState() => _PngHomeState();
}

class _PngHomeState extends State<PngHome> {
  GlobalKey globalKey = GlobalKey();

  Future<void> _capturePng() async {
    RenderRepaintBoundary boundary =
        globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    print(pngBytes);
  }

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      key: globalKey,
      child: Container(
        color: Colors.green,
        child: Center(
          child: FlatButton(
            color: Colors.white,
            child: Text(
              'Hello World',
              textDirection: TextDirection.ltr,
            ),
            onPressed: _capturePng,
          ),
        ),
      ),
    );
  }
}
```

**Reproduction:**
- Start app
- hit "Hello world" button

Optional for wondering:
- hit "Hello world" button again, then it is working.

Expected: No exception on the first time hitting the button.