0

I want to close my Flutter my app programmatically, I have tried all available solutions they work pretty well for Android but do not work for iOS.

Below code works well for Android but not for iOS

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: GestureDetector(
              onTap: () {
                Future.delayed(const Duration(milliseconds: 2000), () {
                  SystemNavigator.pop();
                  print('object'); 
                });

                
              },
              child: Text('Restart')),
        ),
      ),
    );
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
HMAD
  • 1
  • 1
    Does this answer your question? [Flutter how to programmatically exit the app](https://stackoverflow.com/questions/45109557/flutter-how-to-programmatically-exit-the-app) – Soveyyy May 03 '23 at 08:30

2 Answers2

0

exit(0); will work, if you want to both exit (pop) and terminate the app. Not the most elegant user experience, but some apps do do it.

Mike Irving
  • 1,480
  • 1
  • 12
  • 20
  • Apple will surely reject your app for this reason. – Ujjawal Maurya May 03 '23 at 09:24
  • @UjjawalMaurya it depends on the app. it is not forbidden, some apps do do it. However, it is not usually in the realms of a good user experience, so it depends on what the intention of the app exit is. – Mike Irving May 03 '23 at 09:58
0

iOS doesn't allows app to close by itself. If you use exit(0), Apple will reject your application because it terminates DartVM and looks like app just crashed.

Apple doesn't allows that.

Ujjawal Maurya
  • 462
  • 5
  • 17