添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Noob here. I've made a update checker with flutter, but if I choose any button, it give me black screen. How can I fix this? Any ideas?

  • Full Source : https://github.com/aroxu/LiteCalculator
  • Dialog Part Source :
  • import 'package:LiteCalculator/updater/bean/UpdaterBean.dart';
    import 'package:flutter/material.dart';
    class UpdateHolder extends StatelessWidget {
      final List<Version> version;
      UpdateHolder({Key key, this.version}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return calculateResult(
            version[0].latestVersion, version[1].currentVersion, context);
      Widget calculateResult(latestVersion, currentVersion, context) {
        print('Latest Version : ${int.parse(latestVersion)}');
        print('Current Version : ${int.parse(currentVersion)}');
        Widget data;
        if ((int.parse(currentVersion) <= int.parse(latestVersion))) {
          data = Center(
            child: createAlert('Update Required', actions: <Widget>[
              FlatButton(
                child: Text('OK'),
                onPressed: () {
                  print('OK Button Pressed.');
                  Navigator.of(context).pop();
              FlatButton(
                child: Text('Later'),
                onPressed: () {
                  print('Later Button Pressed.');
                  Navigator.of(context).pop();
        } else
          data = Center();
        return data;
      Widget createAlert(content, {List<Widget> actions, title}) {
        AlertDialog snackBar;
        snackBar = AlertDialog(
          content: Text(content),
          actions: actions,
        return snackBar;
                    You are not using dialog. you just displaying an widget in the very first screen. If you do Navigator.pop the only screen will be removed from the stack. thats why you are getting black screen.
    – Crazy Lazy Cat
                    Jan 17, 2020 at 6:27
                    @CrazyLazyCat I want to if Later button clicked, just close the alertdialog and if OK clicked, open a webpage.
    – B1ackAnge1
                    Jan 17, 2020 at 6:31
    class MyApp extends StatelessWidget {
      Widget build(BuildContext context) {
        return MaterialApp(
          home: FirstPage(),
          debugShowCheckedModeBanner: false,
    class FirstPage extends StatefulWidget {
      @override
      _FirstPageState createState() => _FirstPageState();
    class _FirstPageState extends State<FirstPage> {
      @override
      void initState() {
        _checkUpdate();
        super.initState();
      Future<void> _checkUpdate() async {
        await Future.delayed(Duration.zero);
        await showDialog(
          context: context,
          builder: (context) => UpdateDialog(),
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Text("First Page"),
    class UpdateDialog extends StatefulWidget {
      @override
      _UpdateDialogState createState() => _UpdateDialogState();
    class _UpdateDialogState extends State<UpdateDialog> {
      Future<void> _updateFound;
      @override
      void initState() {
        _updateFound = _checkForUpdate();
        super.initState();
      Future<bool> _checkForUpdate() async {
        await Future.delayed(Duration.zero);
        bool updateFound = false;
        await Future.delayed(Duration(seconds: 3)); // Do Get call to server
        updateFound = true;
        if (!updateFound) Navigator.pop(context);
        return updateFound;
      Future<void> _openWebPage() async {
        Navigator.pop(context);
        launch("https://play.google.com"); //Your link `url_launcher` package
      void _laterClicked(){
        Navigator.pop(context);
      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
          future: _updateFound,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return AlertDialog(
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    CircularProgressIndicator(),
                    const SizedBox(height: 12.0),
                    Text("Checking for Update"),
            else if (snapshot.hasError)
              return AlertDialog(
                title: Text("Error Occured"),
                content: Text("ERROR: ${snapshot.error}"),
            else if(snapshot.data)
              return AlertDialog(
                title: Text("Update Required"),
                content: Text(
                    "Latest version found. Need an update. bla bla bla bla bla bla"),
                actions: <Widget>[
                  FlatButton(
                    child: Text("OK"),
                    onPressed: _openWebPage,
                  FlatButton(
                    child: Text("LATER"),
                    onPressed: _laterClicked,
              return const SizedBox();
    

    context represents the context of the widget, itself (provided in the build method).

    To resolve this issue instead of creating a Dialog widget and returning it as the main widget, just use showDialog and return a simple Container().

    Use dialogContext to pop the dialog and not the widget itself.

    for example:

     if ((int.parse(currentVersion) <= int.parse(latestVersion))) {
         showDialog(
           builder: (dialogContext) => AlertDialog(
            content: Text('Update Required'),
           actions: <Widget>[
             FlatButton(
                child: Text('OK'),
                onPressed: () {
                  print('OK Button Pressed.');
                  Navigator.of(dialogContext).pop();
              FlatButton(
                child: Text('Later'),
                onPressed: () {
                  print('Later Button Pressed.');
                  Navigator.of(dialogContext).pop();
    return Container();
    

    I had a similar problem and my solution was something like that:

    bool hasBeenShown = false;
    if(!hasBeenShown) {
        Navigator.pop(context);
    hasBeenShown = true;
    

    The problem for me was that for some reason Navigator.pop been invoked multiple times when it's supposed to be invoked only once.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.