RenderAspectRatio has unbounded constraints

2,649

I had the same problem, I realized the error code gives me the solution, put the AspectRatio inside a container widget and specify its width and height. In your case, the code below will work out

Container(
    width = your width;
    height = your height;
    child:AspectRatio (
        aspectRatio: widgetAspectRatio,
        child: LayoutBuilder(builder: (context, constraints) {
            var width = constraints.maxWidth;
            var height = constraints.maxHeight;
        }
    )
)
Share:
2,649
ahmed osman
Author by

ahmed osman

Updated on December 10, 2022

Comments

  • ahmed osman
    ahmed osman over 1 year

    I'm trying to build a scroll slider in a flutter app using this code to give to build the constraints:

    var cardAspectRatio = 12.0 / 16.0;
    var widgetAspectRatio = cardAspectRatio * 1.2;
    
    AspectRatio (
    aspectRatio: widgetAspectRatio,
      child: LayoutBuilder(builder: (context, constraints) {
        var width = constraints.maxWidth;
        var height = constraints.maxHeight;
    })
    

    In order to give it its width and height constraints but the resulting error says that the box constraints are unbounded:

    I/flutter ( 6957): RenderAspectRatio has unbounded constraints.
    I/flutter ( 6957): This RenderAspectRatio was given an aspect ratio of 
                       0.8999999999999999 but was given both unbounded
    I/flutter ( 6957): constraints: BoxConstraints(unconstrained)
    I/flutter ( 6957): size: MISSING
    I/flutter ( 6957): aspectRatio: 0.9
    

    How can i solve this?