1

I have a GridView created using data from an SQLite database. Since the data comes from a database I have to use FutureBuilder. I need to find the height of the GridView but findRenderObject() always returns an error (findRenderObject() called on null). I tried using an async function and WidgetsBinding.instance.addPostFrameCallback but it still doesn't work. Is there a way to find the height of a widget rendered with a FutureBuilder?

class _example extends State<example> {
Future grid;
GlobalKey _gridKey = GlobalKey();
@override
  void initState() {
    super.initState();
    grid = getGrid('data.db');
    WidgetsBinding.instance.addPostFrameCallback((_) => showOverlay(context));
  }

void showOverlay(BuildContext context) {
    print('grid height from overlay: '+_getGridHeight().toString());//doesn't work   
    OverlayState overlayState = Overlay.of(context);
    entry = OverlayEntry(
      builder: (context) => Positioned(
        left: 0.0,
        top: 500.0,//I NEED GRID HEIGHT TO PLACE OVERLAY AT THE RIGHT SPOT
        bottom: 0.0,
        width: (MediaQuery.of(context).size.width),
        child: QPanel(dbName: widget.xwordnumber),
      ),
    );
    overlayState.insert(entry);
  }

_getGridHeight(){//changing it to async makes no difference
    final RenderBox renderBoxGrid = _gridKey.currentContext.findRenderObject();
    final gridHeight = renderBoxGrid.size.height;
    final roundedHeight = gridHeight.floorToDouble();
    print('roundedHeight: '+roundedHeight.toString());
    return roundedHeight;
  }
@override
  Widget build(BuildContext context) {
    return Scaffold(
    //key: _gridKey, if the key is here it works but I need GridView height NOT Scaffold height
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: FutureBuilder(
          future: grid,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Align(
                 alignment: Alignment.topCenter,
                  child: Container(
                    margin: EdgeInsets.zero,
                    child: GridView.count(
                      padding: EdgeInsets.only(left:4.0,top:5.0,right:4.0,bottom: 0.0),
                      key: _gridKey,//doesn't work
                      shrinkWrap: true,
                      crossAxisCount: 15,
                      mainAxisSpacing: 2,
                      crossAxisSpacing: 2,
                      children: List.generate(snapshot.data.length, (index) {
                       return FocusScope(
                            node: _node,
                            child: Container(
                              decoration: BoxDecoration(
                                  color: snapshot.data[index] == "#" ? Colors.black : Colors.white,
                                  border: Border.all(color: Colors.black)),
                              child:
                              snapshot.data[index] == "#"
                                  ? Text('#')
                                  : _isTextField(snapshot.data[index], index),
                            ));
                      }),
                    ),
                  ) );
            } else if (snapshot.hasError) {
              return Text("${snapshot.error}");
            }
            return CircularProgressIndicator();
          },
        ),
      ),
    );
  }

}

I found this question: Flutter best way to get size/position of widget when it doesn't render which is the same as mine bur it has no answer...

EDIT :
tried this:

_getGridHeight(){
    Future.microtask(() {
    final RenderBox renderBoxGrid = _gridKey.currentContext.findRenderObject() as RenderBox;
    if(renderBoxGrid?.size != null){
    print('Grid Height: '+renderBoxGrid.size.height.toString());
    return renderBoxGrid?.size ?? Size.zero;
    }
   });
  }

...but it still complains of findRenderObject being called on null...

UPDATE
I cannot believe I got NO ANSWER AT ALL...nobody has ever run into a similar problem? I think an answer to this question may be beneficial to many people but maybe I'm wrong...

emiliano67
  • 161
  • 1
  • 8

0 Answers0