1

The answer here shows exiting application through Widget.

I want to know, if I can exit from initPlatformState(){...}

Specifically to my case I have bool response in initPlatformState(){...} i.e., bool reqperm = await SimplePermissions.requestPermission(permission);

What I want is that if reqperm is false, Application exits.

Complete code :

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

}

What can I do to achieve it? I am just newbie to flutter. Thanks for any help.

While SimplePermission is an awesome flutter plugin

Sahdeep Singh
  • 1,342
  • 1
  • 13
  • 34

1 Answers1

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

}
Andrious Solutions
  • 706
  • 1
  • 7
  • 23