Home > other >  show AlertDialog based on condition
show AlertDialog based on condition

Time:08-28

I want to show a What's New style AlertDialog to inform users what has changed in my app after updating. I've created the following function to see if the app has been updated:

  Future<bool> checkNewVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String appVersion = packageInfo.version;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final String? currVersion = prefs.getString("version");

    print("App version: $appVersion");
    print("Current version: $currVersion");

    if (currVersion == null) {
      await prefs.setString("version", appVersion);
      return true;
    }

    if (currVersion != appVersion) return true;

    return false;
  }

When this function is called in the build method below, the print statements output the following, but the alert dialog is not shown:

flutter: App version: 2.0
flutter: Current version: null
  @override
  Widget build(BuildContext context) {
    if (checkNewVersion() == true) {
      showDialog(context: context, builder: (_) =>
      AlertDialog(
        title: const Text("What's New / Que ha Cambiado"),
        content: Text(updateInfo),
        actions: <Widget>[
          TextButton(
            child: const Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          )
        ],
      ));
    }
    return Scaffold(
      // app main menu
      ...
    );
  }

CodePudding user response:

your checkNewVersion is an async function so you have to wait for its result, Try this:

@override
  Widget build(BuildContext context) {
    bool result = await checkNewVersion();
    if (result) {
      showDialog(context: context, builder: (_) =>
      AlertDialog(
        title: const Text("What's New / Que ha Cambiado"),
        content: Text(updateInfo),
        actions: <Widget>[
          TextButton(
            child: const Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          )
        ],
      ));
    }
    return Scaffold(
      // app main menu
      ...
    );
  }

CodePudding user response:

You are Performing an async operation to get the result. So when performing an async function that returns some Future you must await for it otherwise it will return you an incomplete future.

So when calling checkNewVersion()

You should await for its result like

var versionResult = await checkNewVersion();

Your code will be like

 @override
  Widget build(BuildContext context) {
    var versionResult = await checkNewVersion();
    if (versionResult) {
      showDialog(context: context, builder: (_) =>
      AlertDialog(
        title: const Text("What's New / Que ha Cambiado"),
        content: Text(updateInfo),
        actions: <Widget>[
          TextButton(
            child: const Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          )
        ],
      ));
    }
    return Scaffold(
      // app main menu
      ...
    );
  }
  • Related