TimeToCode
TimeToCode

Reputation: 1848

A build function returned null , FutureBuilder

I am trying to create a quiz app, in which when user click on one card, let say Python card so user will be redirected to python questions screen.

for this i found "futurebuilder" which is used to do this.

here is the class loadjson where i'm using futurebuilder and passing a json file to get the questions related to user selected card.

class loadjson extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: DefaultAssetBundle.of(context)
            .loadString("assets/questions/generalQ.json"),
        // ignore: missing_return
        builder: (context, snapshot) {
          List mydata = json.decode((snapshot.data.toString()));
          if (mydata == null) {
            Center(
              child: Text("Loading"),
            );
          } else {
            return quizpage(mydata: mydata);
          }
        },
      ),
    );
  }
}

class quizpage extends StatefulWidget {
  final mydata;
  quizpage({Key key, @required this.mydata}) : super(key: key);

  @override
  _quizpageState createState() => _quizpageState(mydata: mydata);
}

and here i'm passing data which i get from loadjson

class _quizpageState extends State<quizpage> {
  List mydata;
  _quizpageState({this.mydata});

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
      },
      child: Scaffold(
        body: Center(
          child: Text(mydata.toString()),
        ),
      ),
    );
  }
}

ERROR: I'm getting error in return WillPopScope( onWillPop: () {}, this WillPopScope method which is saying

 Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building FutureBuilder<String>(dirty, state: _FutureBuilderState<String>#893e9):
A build function returned null.

The offending widget is: FutureBuilder<String>
Build functions must never return null.

To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".

The relevant error-causing widget was
FutureBuilder<String>
lib\quizpage.dart:9
When the exception was thrown, this was the stack
#0      debugWidgetBuilderValue.<anonymous closure>
package:flutter/…/widgets/debug.dart:305
#1      debugWidgetBuilderValue
package:flutter/…/widgets/debug.dart:326
#2      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4500
#3      StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4667
#4      Element.rebuild
package:flutter/…/widgets/framework.dart:4189
...
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
A build function returned null.
The relevant error-causing widget was
FutureBuilder<String>
lib\quizpage.dart:9

this is android screen error enter image description here

how to get out from this error ?please help!

Upvotes: 1

Views: 1107

Answers (2)

Antonio Valentic
Antonio Valentic

Reputation: 370

Your FutureBuilder indeed returns null if mydata == null, so you just need to add return keyword just before Center widget in that IF statement's body. Like this:

class loadjson extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: DefaultAssetBundle.of(context)
            .loadString("assets/questions/generalQ.json"),
        // ignore: missing_return
        builder: (context, snapshot) {
          List mydata = json.decode((snapshot.data.toString()));
          if (mydata == null) {
            return Center(
              child: Text("Loading"),
            );
          } else {
            return quizpage(mydata: mydata);
          }
        },
      ),
    );
  }
}

Upvotes: 2

DonnC
DonnC

Reputation: 67

class quizpage extends StatefulWidget {
  final mydata;
  quizpage({Key key, @required this.mydata}) : super(key: key);

  @override
  _quizpageState createState() => _quizpageState();
}

class _quizpageState extends State<quizpage> {

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
      },
      child: Scaffold(
        body: Center(
          child: Text('${widget.mydata.length}'),
        ),
      ),
    );
  }
}


Upvotes: 0

Related Questions