我想使用FutureBuilder来构建listView,但是当我运行该应用程序时,会出现一条错误消息:构建函数返回null。
我需要的数据是一个列表,我想首先从本地数据库加载它,如果失败,那么我将发出请求并从网络获取结果。代码如下:
class AllConferencesPage extends StatefulWidget {
@override
AllConferencesState createState() => new AllConferencesState();
}
class AllConferencesState extends State<AllConferencesPage> {
List<Conference> _conferences;
Future _future;
Future<List<Conference>> loadConferencesFromDatabase() async {
List<Conference> conferences;
conferences = await ConferenceProvider.instance.getConferences('1 = 1');
await ConferenceProvider.instance.close();
return conferences;
}
Future<List<Conference>> loadConferencesFromNetworkResponse(
Response response) async {
List<Conference> conferences = new List();
// ...
return conferences;
}
Future<List<Conference>> loadConferences() async {
_future = loadConferencesFromDatabase();
_future.timeout(Duration(seconds: 3),
onTimeout: () async {
Response response = await Dio().get(urlPrefix);
_future = loadConferencesFromNetworkResponse(response);
});
return _future;
}
Widget _buildConferences(List<Conference> conferences) {
return ListView.builder(
shrinkWrap: true,
itemCount: conferences.length,
itemBuilder: (context, i) {
return _buildCard(conferences[i]);
},
padding: const EdgeInsets.all(12.0),
);
}
Widget _buildBlank() {
return Center(
child: CircularProgressIndicator(),
);
}
@override
void initState() {
super.initState();
_future = loadConferences();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Hello Flutter'),
),
body: SafeArea(
child: FutureBuilder<List<Conference>>(
builder: (context, AsyncSnapshot<List<Conference>> snap) {
if (snap.connectionState == ConnectionState.none || snap.connectionState == ConnectionState.waiting || snap.connectionState == ConnectionState.active) {
return _buildBlank();
} else if (snap.connectionState == ConnectionState.done) {
if (snap.hasError) {
return _buildBlank();
} else if (snap.hasData) {
if (snap.data != null) {
_conferences = snap.data;
return _buildConferences(_conferences);
} else {
return _buildBlank();
}
}
}
},
future: _future,
),
),
);
}
}
答案 0 :(得分:0)
如错误消息所述,您必须在构建器函数中返回内容。
只需添加else
大小写并返回Container()
或Center(child: CircularProgressIndicator())
编辑:
if (snap.connectionState == ConnectionState.none || snap.connectionState == ConnectionState.waiting || snap.connectionState == ConnectionState.active) {
return _buildBlank();
} else if (snap.connectionState == ConnectionState.done) {
if (snap.hasError) {
return _buildBlank();
} else if (snap.hasData) { // hasData implies data is not null
_conferences = snap.data;
return _buildConferences(_conferences);
} else {
return _buildBlank();
}
}
答案 1 :(得分:0)
我认为问题是由于在if
函数中使用了builder
语句。运行该函数时,将执行第一个if
语句,但是那时连接状态正在等待,该函数不返回任何内容。要解决此问题,请改用switch
。它为我工作。我不知道我是否在解释正确,但您应该尝试一下。
代码:-
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Hello Flutter'),
),
body: SafeArea(
child: FutureBuilder<List<Conference>>(
builder: (context, AsyncSnapshot<List<Conference>> snap) {
switch(snap.connectionState) {
case ConnectionState.waiting:
return _buildBlank();
default:
if (snap.hasError) {
return _buildBlank();
} else if (snap.hasData) {
if (snap.data != null) {
_conferences = snap.data;
return _buildConferences(_conferences);
} else {
return _buildBlank();
}
}
}
},
future: _future,
),
),
);
}