【问题标题】:how to check version on the app lauching?如何在应用程序启动时检查版本?
【发布时间】:2021-08-19 00:18:25
【问题描述】:

我有检查 Flutter 应用程序版本的代码,但它包含一个我无法在 buildcontext 方法中插入的异步代码,所以我决定将它放入 main 方法并插入一个弹出窗口告诉用户他必须更新应用程序,但我收到此响应:断言失败:第 70 行 pos 15: context!=null:is not true 因为我在我的 alertDialog 中将上下文设置为 null,所以我不知道在哪里放置它这样每次用户启动应用程序时,版本检查就会运行,如果他拥有的版本低于商店中的版本,则会显示 alertDialog。这是我检查它的代码以及我如何实现它: 我使用NewVersion 包来检查并比较谷歌播放中的本地版本和存储版本

  class PataStoreConnector extends StatefulWidget {
  final bool loggedin;
  PataStoreConnector({Key key, this.loggedin}) : super(key: key);

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

class _PataStoreConnectorState extends State<PataStoreConnector> {
  bool sleeping = true;
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      timer();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (sleeping) {
      return PataSplashScreen();
    }
    return StoreConnector<PataState, PataViewModel>(
      converter: PataViewModel.convertStateToViewModel,
      builder: (BuildContext context, PataViewModel pataViewModel) {
        return getPage(pataViewModel);
      },
    );
  }

以及包含版本检查代码的getPage方法:

   // ignore: missing_return
  Widget getPage(PataViewModel viewModel) {
    //Pt.instance.version;

final newVersion = NewVersion(
  androidId: 'com.snedac.empata',
  context: context,
);
// setState(() async {
//   VersionStatus vs = await newVersion.getVersionStatus();
// });
newVersion.getVersionStatus().then((result) {
  print("store version ${result.storeVersion}");
  print("local version ${result.localVersion}");
  if (result.storeVersion == result.localVersion) {
    if (viewModel.isLoggingIn()) {
      viewModel.refreshUserData();
      // vm.setIsHomeLoadedStateAction(true);
      return PataSplashScreen();
    }

    if (!viewModel.isLoggedIn()) {
      return LoginPage(
        viewModel: viewModel,
      );
    }
    print("homeloaded:${viewModel.state.isHomeloaded}");
    print("userIdLogin:${viewModel.state.user.userId}");
    if (viewModel.state.isHomeloaded == false) {
      if (viewModel.state.user.userId > 0) {
        viewModel.initHome();
      } else {
        return LoginPage(
          viewModel: viewModel,
        );
      }
    }
    return PataHome(title: "e-Mpata");
  } else {
    _ackAlert2("Mise a jour", 'Votre version est trop obsolète.');
  }
});

}

【问题讨论】:

标签: flutter dart


【解决方案1】:

您永远不需要在build() 方法中执行async 工作。如果您想在启动时显示过期版本的错误,您可以执行如下代码:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();  // make sure plugins are initialized properly
  final isOutOfDate = await checkAppOutOfDate();  // do the check
  runApp(MyApp(isOutOfDate: isOutOfDate));  // pass the boolean to your app as a parameter
}

然后,您可以修改MyApp()(或您的小部件树根部的任何内容)以接受此参数并正确处理:

class MyApp extends StatefulWidget {
  final bool isOutOfDate;
  MyApp({required this.isOutOfDate});

  @override
  MyAppState createState() => MyAppState();

}

class MyAppState extends State<MyApp> {
  
  // a bit like initState(), but has access to a live BuildContext
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    
    if (widget.isOutOfDate) {
      showDialog(context, ...);  // show a dialog to the user with a proper BuildContext
    }
  }

  @override
  Widget build(BuildContext context) {
    ...
  }
}

如果您希望在initState 中显示对话框,则必须延迟显示它,例如:

@override
void initState() {
  super.initState();
  SchedulerBinding.instance!.addPostFrameCallback((_) => showDialog(context, ...));
}

但在这种情况下,两者都会达到相似的结果

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 2013-06-17
    • 2011-03-11
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多