Skip to content

Null check error when using "Show Debug Paint" for some widgets #105969

@ChristianEdwardPadilla

Description

@ChristianEdwardPadilla

Internal: b/235913015

Error is thrown when using "Show Debug Paint" from Android Studio. The issue is reproducible when using some specific widgets, e.g. SliverAppBar, BottomAppBar.

Steps to Reproduce

  1. Start sample app (below)
  2. Enable debug paint
  3. Observe crash

SliverAppBar example app

import 'package:flutter/material.dart';
 
void main() => runApp(const MyApp());
 
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
 
  static const String _title = 'Flutter Code Sample';
 
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}
 
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);
 
  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
 
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool _pinned = true;
  bool _snap = false;
  bool _floating = false;
 
// [SliverAppBar]s are typically used in [CustomScrollView.slivers], which in
// turn can be placed in a [Scaffold.body].
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            pinned: _pinned,
            snap: _snap,
            floating: _floating,
            expandedHeight: 160.0,
            flexibleSpace: const FlexibleSpaceBar(
              title: Text('SliverAppBar'),
              background: FlutterLogo(),
            ),
          ),
          const SliverToBoxAdapter(
            child: SizedBox(
              height: 20,
              child: Center(
                child: Text('Scroll to see the SliverAppBar in effect.'),
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  color: index.isOdd ? Colors.white : Colors.black12,
                  height: 100.0,
                  child: Center(
                    child: Text('$index', textScaleFactor: 5),
                  ),
                );
              },
              childCount: 20,
            ),
          ),
        ],
      ),
      bottomNavigationBar: BottomAppBar(
        child: Padding(
          padding: const EdgeInsets.all(8),
          child: OverflowBar(
            overflowAlignment: OverflowBarAlignment.center,
            children: <Widget>[
              Row(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const Text('pinned'),
                  Switch(
                    onChanged: (bool val) {
                      setState(() {
                        _pinned = val;
                      });
                    },
                    value: _pinned,
                  ),
                ],
              ),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const Text('snap'),
                  Switch(
                    onChanged: (bool val) {
                      setState(() {
                        _snap = val;
                        // Snapping only applies when the app bar is floating.
                        _floating = _floating || _snap;
                      });
                    },
                    value: _snap,
                  ),
                ],
              ),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const Text('floating'),
                  Switch(
                    onChanged: (bool val) {
                      setState(() {
                        _floating = val;
                        _snap = _snap && _floating;
                      });
                    },
                    value: _floating,
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Error thrown:

======== Exception caught by rendering library =====================================================
The following _CastError was thrown during paint():
Null check operator used on a null value
 
The relevant error-causing widget was: 
  SliverAppBar SliverAppBar:google3:///mobile/flutter/samples/hello_flutter/app/lib/main.dart:38:11
When the exception was thrown, this was the stack: 
#0      RenderClipPath.debugPaintSize.<anonymous closure> (package:flutter/src/rendering/proxy_box.dart:1854:38)
#1      RenderClipPath.debugPaintSize (package:flutter/src/rendering/proxy_box.dart:1858:6)
#2      RenderBox.debugPaint.<anonymous closure> (package:flutter/src/rendering/box.dart:2725:9)
#3      RenderBox.debugPaint (package:flutter/src/rendering/box.dart:2734:6)
#4      RenderObject._paintWithContext.<anonymous closure> (package:flutter/src/rendering/object.dart:2722:7)

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: devtoolsDevTools related - suite of performance and debugging toolsa: null-safetySupport for Dart's null safety featurec: crashStack traces logged to the consolecustomer: money (g3)f: inspectorPart of widget inspector in framework.found in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions