HMAD
HMAD

Reputation: 1

How do I close my Flutter app programmatically on iOS?

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')),
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 1967

Answers (2)

Ujjawal Maurya
Ujjawal Maurya

Reputation: 616

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.

Upvotes: 0

Mike Irving
Mike Irving

Reputation: 1630

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.

Upvotes: -1

Related Questions