答案here显示通过Widget退出的应用程序。
我想知道是否可以从initPlatformState(){...}
退出
根据我的情况,我在initPlatformState(){...}
(即bool reqperm = await SimplePermissions.requestPermission(permission);
我想要的是,如果reqperm为false,则应用程序退出。
完整代码:
class StartPlayer extends StatefulWidget{
@override
appState createState() => new appState();
}
class appState extends State<StartPlayer>{
String _platformVersion = 'Unknown';
Permission permission;
Songs songS;
bool loading = true;
var allsongs;
@override
void initState() {
// TODO: implement initState
super.initState();
initPlatformState();
}
initPlatformState() async {
loading = true;
String platformVersion;
try {
platformVersion = await SimplePermissions.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
bool chkperm = await SimplePermissions.checkPermission(permission);
if(chkperm) {
try {
allsongs = await methodsMP.allSongs();
} catch (e) {
print("Fials to load songs : '${e.message}'.");
}
}
else{
bool reqperm = await SimplePermissions.requestPermission(permission);
if(reqperm){
try {
allsongs = await methodsMP.allSongs();
} catch (e) {
print("Fials to load songs : '${e.message}'.");
}
}
else{
//I want to exit App Now.Cause setState depends upon allsongs
}
}
loading= false;
print(allsongs);
if (!mounted) return;
setState((() {
songS = new Songs(new List.from(allsongs));
}));
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
songS.audioPlayer.stop();
}
@override
Widget build(BuildContext context) {
return new playerstate(allsongs,loading,new mainView());
}
}
我该怎么做?我只是扑朔迷离的新手。感谢您的帮助。
SimplePermission令人发指,plugin
答案 0 :(得分:0)
import 'dart:io';
class StartPlayer extends StatefulWidget{
@override
appState createState() => new appState();
}
class appState extends State<StartPlayer>{
String _platformVersion = 'Unknown';
Permission permission;
Songs songS;
bool loading = true;
var allsongs;
@override
void initState() {
// TODO: implement initState
super.initState();
initPlatformState();
}
initPlatformState() async {
loading = true;
String platformVersion;
try {
platformVersion = await SimplePermissions.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
bool chkperm = await SimplePermissions.checkPermission(permission);
if(chkperm) {
try {
allsongs = await methodsMP.allSongs();
} catch (e) {
print("Fials to load songs : '${e.message}'.");
}
}
else{
bool reqperm = await SimplePermissions.requestPermission(permission);
if(reqperm){
try {
allsongs = await methodsMP.allSongs();
} catch (e) {
print("Fials to load songs : '${e.message}'.");
}
}
else{
//I want to exit App Now.Cause setState depends upon allsongs
exit(0); // or non-zero for some error code
}
}
loading= false;
print(allsongs);
if (!mounted) return;
setState(() {
songS = new Songs(new List.from(allsongs));
});
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
songS.audioPlayer.stop();
}
@override
Widget build(BuildContext context) {
return new playerstate(allsongs,loading,new mainView());
}
}