生成函数返回null-FutureBuilder

时间:2020-03-12 11:37:04

标签: android flutter dart

我对此感到有些困惑,因为我的未来构建者应该在等待未来的同时返回循环进度指示器。它曾经工作过,但我一直在进行更改而不进行提交,因此无法检查我所做的更改。

未来的建设者:

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"),
                  ),
                ],
             ),
            ),

将来的功能:

  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(),
      ),
    );
  }

预期结果:出现一个下拉菜单

实际结果:一个大红色框表示一个构建函数返回了违反null的小部件FutureBuilder

日志

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.

1 个答案:

答案 0 :(得分:0)

假设snapshot.data返回String,您可以执行以下操作:

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