Flutter:为iOS和Android添加应用更新对话框

3
我目前正在开发通知功能,当有新的更新可用时,用户会收到一个对话框,可以选择更新或不更新。我是使用Firebase Remote Config来完成这个功能的,我有一个名为"force_update_current_version"的参数,然后添加版本值进行检查。但我确实遇到了以下错误。
感谢您的帮助,祝您新年健康。
Main.dart代码
import 'checkUpdate.dart';

@override
void initState() {
  try {
    versionCheck(**context**);
  } catch (e) {
    print(e);
  }
  **super**.initState();
}

上下文错误:未定义名称“context”。 尝试更正名称以使用已定义的名称,或者定义该名称。

super 错误:无效的'super'调用上下文。

checkUpdate.dart 代码

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:package_info/package_info.dart';
import 'package:flutter/cupertino.dart';

const APP_STORE_URL = 'https://apps.apple.com/us/app/appname/idAPP-ID';
const PLAY_STORE_URL =
    'https://play.google.com/store/apps/details?id=APP-ID';

versionCheck(context) async {
  //Get Current installed version of app
  final PackageInfo info = await PackageInfo.fromPlatform();
  double currentVersion = double.parse(info.version.trim().replaceAll(".", ""));

  //Get Latest version info from firebase config
  final RemoteConfig remoteConfig = await RemoteConfig.instance;

  try {
    // Using default duration to force fetching from remote server.
    await remoteConfig.fetch(expiration: const Duration(seconds: 0));
    await remoteConfig.activateFetched();
    remoteConfig.getString('force_update_current_version');
    double newVersion = double.parse(remoteConfig
        .getString('force_update_current_version')
        .trim()
        .replaceAll(".", ""));
    if (newVersion > currentVersion) {
      _showVersionDialog(context);
    }
  } on FetchThrottledException catch (exception) {
    // Fetch throttled.
    print(exception);
  } catch (exception) {
    print('Unable to fetch remote config. Cached or default values will be '
        'used');
  }
}

//Show Dialog to force user to update
_showVersionDialog(context) async {
  await showDialog<String>(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      String title = "New Update Available";
      String message =
          "There is a newer version of app available please update it now.";
      String btnLabel = "Update Now";
      String btnLabelCancel = "Later";
      return Platform.isIOS
          ? new CupertinoAlertDialog(
              title: Text(title),
              content: Text(message),
              actions: <Widget>[
                FlatButton(
                  child: Text(btnLabel),
                  onPressed: () => _launchURL(**Config**.APP_STORE_URL),
                ),
                FlatButton(
                  child: Text(btnLabelCancel),
                  onPressed: () => Navigator.pop(context),
                ),
              ],
            )
          : new AlertDialog(
              title: Text(title),
              content: Text(message),
              actions: <Widget>[
                FlatButton(
                  child: Text(btnLabel),
                  onPressed: () => _launchURL(**Config**.PLAY_STORE_URL),
                ),
                FlatButton(
                  child: Text(btnLabelCancel),
                  onPressed: () => Navigator.pop(context),
                ),
              ],
            );
    },
  );
}

_launchURL(String url) async {
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

应用程序和Play商店的配置错误:未定义名称“Config”。尝试更正为已定义的名称或定义该名称。


你是否已经在Firebase上更改了force_update_current_version的版本? - Shubham Narkhede
1个回答

1
  1. checkUpdate.dart 中,我们需要导入 firebase_remote_config 包,该包公开了 RemoteConfig 类:
import 'package:firebase_remote_config/firebase_remote_config.dart';

在此之前安装它

  1. versionCheck() 函数应该从 StatefulWidget 中调用,因此,一个好的调用位置是第一个屏幕的 Widget 内部,例如:

class FirstScreen extends StatefulWidget {
  const FirstScreen({ Key key }) : super(key: key);

  @override
  _FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  @override
  void initState() {
     super.initState();
     WidgetsBinding.instance
      .addPostFrameCallback((_) => versionCheck(context));
  }
  @override
  Widget build(BuildContext context) {
    return Container(color: const Color(0xFFFFE306));
  }
}

我已经在Pubspec中安装了远程配置包,并在checkUpdate.dart中导入了它,但我仍然无法在两个URL前面添加Config。 - Tempelritter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接