Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RenderBox was not laid out: RenderCustomPaint Flutter

I keep getting this error when I try to run the app :

════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderCustomPaint#534a9 relayoutBoundary=up16 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
‘package:flutter/src/rendering/box.dart’:
box.dart:1
Failed assertion: line 1966 pos 12: ‘hasSize’

The relevant error-causing widget was
ListView
account_screen.dart:32
════════════════════════════════════════════════════════════════════════════════

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderRepaintBoundary#d2a6e relayoutBoundary=up15 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
‘package:flutter/src/rendering/box.dart’:
box.dart:1
Failed assertion: line 1966 pos 12: ‘hasSize’

The relevant error-causing widget was
Column
account_screen.dart:19
════════════════════════════════════════════════════════════════════════════════

here is my code:

body: SingleChildScrollView(
      child: Column(
        children: [
          StreamBuilder(
              stream:
                  FirebaseFirestore.instance.collection('Ads').snapshots(),
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (!snapshot.hasData) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                }

                return ListView(
                    children: snapshot.data!.docs.map((document) {
                  return Card(
                    child: ListTile(
                      leading: Image.asset('assets/images/facebook.png'),
                      title: Text(document['Title']),
                      subtitle: Text(document['Description']),
                      trailing: const Icon(Icons.car_crash),
                    ),
                  );
                }).toList());
              })
        ],
      ),
    ),

>Solution :

By default, ListView tries to use all the available space but this space is infinite, which leads to the error. You can use shrinkWrap property to make the ListView using only the space needed by its children :

return ListView(
  shrinkWrap: true, // Add this
  children: snapshot.data!.docs.map((document) {
  ...
  }
).toList());

More about shrinkWrap here: https://api.flutter.dev/flutter/widgets/ScrollView/shrinkWrap.html

and here https://stackoverflow.com/a/54008230/11244991

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading