【发布时间】:2021-07-20 15:36:26
【问题描述】:
我正在尝试创建一个测验应用程序,当用户单击一张卡片时,假设是 Python 卡片,因此用户将被重定向到 Python 问题屏幕。
为此,我找到了用于执行此操作的“futurebuilder”。
这是 loadjson 类,我在其中使用 futurebuilder 并传递一个 json 文件来获取与用户选择的卡片相关的问题。
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);
}
我在这里传递从 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()),
),
),
);
}
}
错误: 我收到错误 返回 WillPopScope( onWillPop: () {}, 这个 WillPopScope 方法在说
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
如何摆脱这个错误?请帮助!
【问题讨论】:
标签: flutter flutter-futurebuilder