简体   繁体   中英

A build function returned null - FutureBuilder

I'm a bit confused by this because my future builder should be returning a circular progress indicator while its waiting for the future. It worked before but i've been changing things without making commits so cant check what i've changed.

The future builder:

Container(
              child: Row(
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  FutureBuilder(
                    future: BooruSelector(),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.connectionState == ConnectionState.done){
                        return snapshot.data;
                      } else {
                        return Center(child: CircularProgressIndicator());
                      }
                    },
                  ),
                  FlatButton(
                    onPressed: (){
                      if(selectedBooru != null){
                        Get.to(booruEdit(selectedBooru,widget.settingsHandler));
                      }
                      //get to booru edit page;
                    },
                    child: Text("Edit"),
                  ),
                ],
             ),
            ),

The future function:

  Future BooruSelector() async{
    await widget.settingsHandler.getBooru();
    widget.settingsHandler.booruList.add(new Booru("New"," ","https://i.imgur.com/fGHg4Ul.png"," "));
    if (selectedBooru == null){
      selectedBooru = widget.settingsHandler.booruList[0];
    }
    return Container(
      child: DropdownButton<Booru>(
        value: selectedBooru,
        icon: Icon(Icons.arrow_downward),
        onChanged: (Booru newValue){
          print(newValue.baseURL);
          setState((){
            selectedBooru = newValue;
          });
        },
        items: widget.settingsHandler.booruList.map<DropdownMenuItem<Booru>>((Booru value){
          return DropdownMenuItem<Booru>(
            value: value,
            child: Row(
              children: <Widget>[
                Text(value.name),
                Image.network(value.faviconURL,),
              ],
            ),
          );
        }).toList(),
      ),
    );
  }

Expected results: A drop down menu to appear

Actual results: A big red box saying A build function returned null offending widget FutureBuilder

Logs

I/flutter ( 6238): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 6238): The following assertion was thrown building FutureBuilder<dynamic>(dirty, state:
I/flutter ( 6238): _FutureBuilderState<dynamic>#75ce1):
I/flutter ( 6238): A build function returned null.
I/flutter ( 6238): The offending widget is:
I/flutter ( 6238):   FutureBuilder<dynamic>
I/flutter ( 6238): Build functions must never return null.
I/flutter ( 6238): To return an empty space that causes the building widget to fill available room, return
I/flutter ( 6238): "Container()". To return an empty space that takes as little room as possible, return
I/flutter ( 6238): "Container(width: 0.0, height: 0.0)".
I/flutter ( 6238): 
I/flutter ( 6238): The relevant error-causing widget was:
I/flutter ( 6238):   FutureBuilder<dynamic> file:///home/kannalo/AndroidStudioProjects/loli/lib/main.dart:240:19
I/flutter ( 6238): 
I/flutter ( 6238): When the exception was thrown, this was the stack:
I/flutter ( 6238): #0      debugWidgetBuilderValue.<anonymous closure> (package:flutter/src/widgets/debug.dart:280:7)
I/flutter ( 6238): #1      debugWidgetBuilderValue (package:flutter/src/widgets/debug.dart:301:4)
I/flutter ( 6238): #2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4330:7)
I/flutter ( 6238): #3      Element.rebuild (package:flutter/src/widgets/framework.dart:4053:5)
I/flutter ( 6238): #4      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2532:33)
I/flutter ( 6238): #5      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:796:20)
I/flutter ( 6238): #6      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
I/flutter ( 6238): #7      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1109:15)
I/flutter ( 6238): #8      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1048:9)
I/flutter ( 6238): #9      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:964:5)
I/flutter ( 6238): #13     _invoke (dart:ui/hooks.dart:260:10)
I/flutter ( 6238): #14     _drawFrame (dart:ui/hooks.dart:218:3)
I/flutter ( 6238): (elided 3 frames from package dart:async)
I/flutter ( 6238): 
I/flutter ( 6238): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 6238): Another exception was thrown: A RenderFlex overflowed by 99677 pixels on the right.

Assuming snapshot.data returns a String , you can do something like this:

if (snapshot.connectionState == ConnectionState.done){
  return Text(snapshot.data ?? "No data"); // you need to return a widget from here. 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM