Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SizedBox with zero size throws Failed assertion: ... 'width > 0.0': is not true, Proposal to improve error message if SizedBox has zero size #135082

Closed
2 tasks done
bramp opened this issue Sep 20, 2023 · 7 comments · Fixed by #150430
Labels
a: error message Error messages from the Flutter framework c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project r: fixed Issue is closed as already fixed in a newer version team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@bramp
Copy link
Contributor

bramp commented Sep 20, 2023

Is there an existing issue for this?

Steps to reproduce

  1. Write the following
  const Row(
      children: [
        FittedBox(
          child: SizedBox(
            height: 0,
            width: 0,
          ),
        ),
      ],
    ),
  1. Run

Expected results

Nothing to be displayed as the widget is effectively zero sized.

Actual results

The following assertion was thrown during performLayout():
'package:flutter/src/rendering/box.dart': Failed assertion: line 320 pos 12: 'width > 0.0': is not true.
box.dart:320

Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.yml

If I change the SizedBox to have a non-zero size, or take the FittedBox out of the Row, then the assertion goes away. I'm only filing this to improve the error message / handling of zero width elements.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const Row(
        children: [
          FittedBox(
            child: SizedBox(
              height: 0,
              width: 0,
            ),
          ),
        ],
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
flutter doctor -v
[✓] Flutter (Channel stable, 3.13.4, on macOS 13.5.2 22G91 darwin-arm64, locale en-GB)
    • Flutter version 3.13.4 on channel stable at /opt/homebrew/Caskroom/flutter/3.13.2/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 367f9ea16b (7 days ago), 2023-09-12 23:27:53 -0500
    • Engine revision 9064459a8b
    • Dart version 3.1.2
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/bramp/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6 0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6 0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.82.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.73.20230904

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 13.5.2 22G91 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 116.0.5845.187

[✓] Network resources
    • All expected network resources are available.

• No issues found!
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Sep 20, 2023
@darshankawar
Copy link
Member

Thanks for the report. The error occurs due to the fact that the width and height values are 0. If we provide non-zero values, then the error goes away.

The use case is if we provide 0 values then it throws: Failed assertion: line 320 pos 12: 'width > 0.0': is not true. as expected, but OP is proposing to have an improved error message.

Treating the issue as applicable and labeling for team's attention.

@darshankawar darshankawar added framework flutter/packages/flutter repository. See also f: labels. c: proposal A detailed proposal for a change to Flutter team-framework Owned by Framework team and removed in triage Presently being triaged by the triage team labels Sep 20, 2023
@darshankawar darshankawar changed the title Failed assertion: ... 'width > 0.0': is not trueshould provide substantially more information in this error message SizedBox with zero size throws Failed assertion: ... 'width > 0.0': is not true, Proposal to improve error message if SizedBox has zero size Sep 20, 2023
@darshankawar darshankawar added the a: error message Error messages from the Flutter framework label Sep 20, 2023
@saifr383
Copy link

@darshankawar i am new to contribution can i do this change?

@AbhishekDoshi26
Copy link

@darshankawar based on my initial research, this issue is not of SizedBox. This issue only happens when the SizedBox is wrapped with FittedBox.

FittedBox is a widget that tries to size and position its child within the available space while maintaining the child's aspect ratio (kind of similar to ConstrainedBox). When you set the width and height of the SizedBox child to zero, you are essentially telling Flutter to render a child with zero dimensions, which is causing the error.

We need to change the assert message which is present here.

@goderbauer goderbauer added P3 Issues that are less important to the Flutter project triaged-framework Triaged by Framework team labels Sep 26, 2023
@rekire
Copy link
Contributor

rekire commented Nov 14, 2023

@AbhishekDoshi26 this is just the case when the view is in a Row widget, when it is in a Column like in my linked ticket above it should be the line below.

I could also imagine the case that it is in a Stack where both constrains could be broken and it should be fine.

Edit: Might be the parent size should be not zero in all those cases.

@Passer-by
Copy link

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: FittedBox(
          child:Text(''),
        ),
      ),
    );
  }

The same issue persists in this code.

@PurplePolyhedron
Copy link
Contributor

PurplePolyhedron commented Mar 28, 2024

Would it be the correct for a FittedBox to throw if the child has a size of zero, to improve the error message?
Currently if the constraints of FittedBox is tight, and fit is not BoxFit.scaleDown, there is no error for having a child of size 0. In this case throwing would be a breaking change.

Or should FittedBox be skipping child of size 0 and not throw an error?

Also possible work around by wrapping child with ConstrainedBox(constraints: BoxConstraints(minHeight: 1, minWidth: 1),)

@auto-submit auto-submit bot closed this as completed in f194cd3 Jul 8, 2024
@darshankawar darshankawar added the r: fixed Issue is closed as already fixed in a newer version label Jul 9, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: error message Error messages from the Flutter framework c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project r: fixed Issue is closed as already fixed in a newer version team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants