Search code examples
flutterflutter-dependencies

Why am I having this error with PieChart?


first time here asking a question. I don't know how to code but I need an app and don't have the money to pay someone yet.

I've been having the same error again and again but I can't understand why and how to solve it.

here's the code :

Column(
          children: [
            Padding(
              padding: const EdgeInsetsDirectional.only(top: 16),
              child: Container(
                decoration: BoxDecoration(
                    color: const Color.fromARGB(255, 1, 0, 0),
                    borderRadius: BorderRadius.circular(6)),
                width: 300,
                height: 200,
                child: Column(
                  children:[
                    const Text(
                      'Votre Patrimoine',
                      style: TextStyle(color: Colors.white),
                    ),
                    Row(
                      children: [
                        Column(
                          children: [
                            LayoutBuilder(
                              builder: (context, constraints) {
                                return const PieChart(
                                  dataMap: {
                                    '1': 25,
                                    '2': 75,
                                  },
                                  colorList: [
                                    Colors.blue,
                                    Colors.red,
                                  ],
                                );
                              },
                            )

                          ],
                            )
                      ]
                        ),
                        Column(
                          
                          children: [
                            const Text('Les 24 dernières heures',
                            style: TextStyle(color: Colors.white),),
                            Text(dayvariations,
                            style: const TextStyle(color: Colors.white))
                          ]
                          
                        )
                      ],)
                    )
                  ),
                
          ]

here's a part of the error VsCode gives me :

RenderBox was not laid out: RenderPositionedBox#4dc8d relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': package:flutter/…/rendering/box.dart:1 Failed assertion: line 2001 pos 12: 'hasSize'

The relevant error-causing widget was PieChart

I was expecting to have a Piechart


Solution

  • This happens when a widget has no constrained height which makes it gets an infinite height or width.

    PieChart needs a defined or constrained height, you should wrap in another widget like sizedBox or a container with a heigth.

    Like this :

                            Row(children: [
                          Column(
                            children: [
                              LayoutBuilder(
                                builder: (context, constraints) {
                                  return Container(
                                    height: 500,
                                    child: PieChart(
                                      dataMap: {
                                        '1': 25,
                                        '2': 75,
                                      },
                                      colorList: [
                                        Colors.blue,
                                        Colors.red,
                                      ],
                                    ),
                                  );
                                },
                              )
                            ],
                          )
                        ]),