How to finish current view / activity in flutter?

use Navigator.pushReplacement(BuildContext context, Route<T> newRoute) to open a new route which replace the current route of the navigator


Do you mean close current screen and come back to previous screen? If that, Navigator.pop(context) should do the work. However, if you are at the entry screen (the first screen of the app and this screen has no parent screen/previous screen), Navigator.pop(context) will return you to a black screen. In this case, we have to use SystemNavigator.pop().

But don't use SystemNavigator.pop() for iOS, Apple says that the application should not exit itself :)

Below code will work on Android for both cases

if (Navigator.canPop(context)) {
  Navigator.pop(context);
} else {
  SystemNavigator.pop();
}

Tags:

Flutter