它的工作原理如下(或者应该这样): 我使用始终以FALSE(_isMsgVisible = FALSE)开头的Visibility()小部件。在initState()中,我读取了文件,如果没有记录,则显示帮助(_isMsgVisible = TRUE)。如果有记录,则显示带有数据的ListView。 问题在于,当有数据时,帮助会快速出现,并且仅在执行initState()之后消失。 仅在执行initState()之后如何控制显示? 对不起,我的英语不好?
class _PlaylistState extends State<PlaylistScreen>
bool _isMsgVisible = false;
@override
void initState() {
super.initState();
_selectedPage = 1;
_playlistDetails.clear();
_getDetails();
}
Future<Null> _getDetails() async {
_playlistDetails.clear();
final String url = '$URL_PLAYLISTS/read.php?user=${widget.userId}';
final mapResponse = await http.get(url);
final responseJson = json.decode(mapResponse.body);
if (responseJson["result"] == "ok") {
if (mounted) {
setState(() {
for (Map playlist in responseJson["data"]) {
_playlistDetails.add(Playlist.fromJson(playlist));
}
});
}
}
setState(() {
_playlistDetails.length == 0 ? _isMsgVisible = true : _isMsgVisible = false;
});
}
_myPlaylists() {
Expanded e = new Expanded(
child: _playlistDetails.length == 0
? new Visibility(
visible: _isMsgVisible,
child: MyInfoMessageBox(
text:
"Lista somente as playlists criadas por você, podendo ser públicas ou privadas.",
title: "Minhas Playlists",
),
)
: new ListView.builder(
itemCount: _playlistDetails.length,
itemBuilder: (context, index) {
return _buildDismiss(_playlistDetails, index);
},
),
);
setState(() {
if (_playlistDetails.length == 0) {
_isMsgVisible = true;
} else {
_isMsgVisible = false;
}
});
return e;
}