The following assertion was thrown during paint(): RenderBox was not laid out

1.5k views Asked by At

The following assertion was thrown during paint():

RenderBox was not laid out: RenderRepaintBoundary#edc84 relayoutBoundary=up2 NEEDS-PAINT 'package:flutter/src/rendering/box.dart': Failed assertion: line 1982 pos 12: 'hasSize'.

I have this issue only when using following methods:

Future getDocs() async {
        QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("books").get();
        for (int i = 0; i < querySnapshot.docs.length; i++) {
          var a = querySnapshot.docs[i].id;
          docids.add(a);
          print(docids.length);
        }
      }
      Future getimg() async {
        QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("books").get();
        for (int i = 0; i < querySnapshot.docs.length; i++) {
          // DocumentSnapshot snapshot=querySnapshot.docs[i].get('image');
          String a = querySnapshot.docs[i].get('image');
          images.add(a);
          print(images.length);
        }
      }

full code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:untitled1/review.dart';
import 'package:untitled1/widget/viewReview.dart';
import 'bottomnavbar.dart';
import 'login.dart';

class homePage extends StatefulWidget {
  const homePage({Key? key}) : super(key: key);

  @override
  _homePageState createState() => _homePageState();
}
class _homePageState extends State<homePage> {
  Future getDocs() async {
    QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("books").get();
    for (int i = 0; i < querySnapshot.docs.length; i++) {
      var a = querySnapshot.docs[i].id;
      docids.add(a);
      print(docids.length);
    }
  }
  Future getimg() async {
    QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("books").get();
    for (int i = 0; i < querySnapshot.docs.length; i++) {
      // DocumentSnapshot snapshot=querySnapshot.docs[i].get('image');
      String a = querySnapshot.docs[i].get('image');
      images.add(a);
      print(images.length);
    }
  }

  List<String> images = [];
  List<String> docids = [];
  final String email = FirebaseAuth.instance.currentUser!.email.toString();
  @override
  Widget build(BuildContext context) {
    getDocs();
    getimg();
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(title: Text('VPGRAM'),foregroundColor: Colors.white,
        backgroundColor: Colors.green,),
      bottomNavigationBar: bottomNavBar(),
      body:
      Container(
        padding: EdgeInsets.all(12.0),
        child: GridView.builder(
          shrinkWrap: true,
          itemCount: images.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 1,
              crossAxisSpacing: 4.0,
              mainAxisSpacing: 4.0,
          ),
          itemBuilder: (BuildContext context, int index){
            String dcid =docids[index];
            return Container(
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Colors.blue,
                  ),
                  borderRadius: BorderRadius.circular(10.0),
                ),
                child:Column(
                  children: <Widget>[
                    IconButton(
                      icon: Image.network(images[index]),
                      iconSize: 300,
                      onPressed: () {
                        Navigator.of(context).push(MaterialPageRoute(
                            builder: (context) => Review(docid: dcid)
                        )
                        );
                      },
                    ),
                    Row(
                        children: [
                          const Text('Book Name'),
                          ElevatedButton(
                              onPressed: () {
                                Navigator.of(context).push(MaterialPageRoute(
                                    builder: (context) => getReview(
                                      docid: dcid,)
                                )
                                );
                              },
                              child: const Text(
                                'view review',
                                style: TextStyle(
                                  decoration: TextDecoration.underline,
                                  color: Color(0xff4c505b),
                                  fontSize: 18,
                                ),
                              )),
                        ]

                    )

                  ],
                ));
          },
        ),
      ),
    );
  }
}
1

There are 1 answers

3
belinda.g.freitas On

Try to add height and weight to the first Container inside itemBuilder: (BuildContext context, int index){}

And if it doesn't resolve the problem, add them to the Container that wrap the whole Scaffold body. Example:

body: Container(
  height: MediaQuery.of(context).size.height, 
  width: MediaQuery.of(context).size.width,
  //rest of code
);

And it would be best to call getDocs() and getimg() inside initState() if you only want to call them once.