flutter showdialog setState() 或 markNeedsBuild() 在构建期间调用

Far*_*ruk 6 showdialog flutter

我有一个 Streambuilder,它读取 Firestore 数据库,然后根据该数据库做出反应。当前列出了我的数据库中的一些信息,但是我想为数据库中出现的特定值添加一个弹出屏幕,当我添加一个 showdialog 函数时,它开始给我

\n
    \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 Exception caught by widgets library \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nsetState() or markNeedsBuild() called during build.\n
Run Code Online (Sandbox Code Playgroud)\n

错误。我怎样才能解决这个问题?我的代码如下。提前致谢。

\n
//This is the function causing errors.      \n_showMaterialDialog (String type) {\n        if(type=="win"){\n          gameResult = "You Win, Gratz!";\n        }else if(type=="lose"){\n          gameResult = "You Lose :(";\n        }\n        print("buraya girdi");\n        print(gameResult);\n        showDialog(\n            context: context,\n            builder: (_) => AlertDialog(\n              title: Text("Result"),\n              content: Text(gameResult),\n              actions: <Widget>[\n                FlatButton(\n                  child: Text('Close'),\n                  onPressed: () {\n                    //Navigator.of(context).pop();\n                    Navigator.pushNamed(context, HomeScreen.id);\n                  },\n                )\n              ],\n            ));\n      }\n    \n      @override\n      Widget build(BuildContext context) {\n        if(_error) {\n          return Text('error-game', textDirection: TextDirection.ltr);\n        }\n    \n        // Show a loader until FlutterFire is initialized\n        if (!_initialized) {\n          return Text('Loading', textDirection: TextDirection.ltr);\n        }\n        return StreamBuilder<DocumentSnapshot>(\n          stream: userSnapshot,\n          builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {\n            if (snapshot.hasError) {\n              return Text('Something went wrong');\n            }\n    \n            if (snapshot.connectionState == ConnectionState.waiting) {\n              return Text("Loading");\n            }\n            if(snapshot.hasData){\n              Map<String, dynamic> userDocument  = snapshot.data.data();\n              print(collectionPath);\n              print(docPath);\n              print(snapshot.data);\n              print(userDocument);\n              gameResult = userDocument['status'];\n\n//this is place function called\n\n              if(gameResult =="win" || gameResult =="lose"){\n                return _showMaterialDialog(gameResult);\n              }\n\n\n              kullanicisayilari = userDocument['atilansayi'];\n              List<dynamic> kullanicisayilariDuz = [];\n              List<dynamic> rakipsayilariDuz = [];\n              List<dynamic> sonuclarDuz = [];\n              for (var numbers in kullanicisayilari){\n                var splittedNumber = numbers.split('|');\n                kullanicisayilariDuz.add(splittedNumber[0]);\n              }\n              rakipsayilari = userDocument['rakipsallama'];\n              sonuc = userDocument['sonuc'];\n              for (var sonuclar in sonuc){\n                var splittedSonuc = sonuclar.split('|');\n                sonuclarDuz.add(splittedSonuc[0]);\n              }\n              for (var rakipsayi in rakipsayilari){\n                var splittedRakipSayi = rakipsayi.split('|');\n                rakipsayilariDuz.add(splittedRakipSayi[0]);\n              }\n              print(myNumbers.decimals);\n              return MaterialApp(\n                home:Scaffold(\n                  appBar: AppBar(\n                    backgroundColor: Colors.amberAccent,\n                    title: Text('Say\xc4\xb1 Av\xc4\xb1 Oyun Ekran\xc4\xb1'),\n                  ),\n                  body:Column(\n                    children: <Widget>[\n                      Expanded(\n                        flex: 80,\n                        child: Row(\n                          children: <Widget>[\n                            Expanded(\n                              flex: 40,\n                              child: Column(\n                                children: <Widget>[\n                                  for(var numbers in kullanicisayilariDuz)Text(numbers),\n                                  ]\n                              ),\n                            ),\n                            Expanded(\n                              flex: 10,\n                              child: Column(\n                                  children: <Widget>[\n                                    for(var numbers in sonuclarDuz)Text(numbers),\n                                  ]\n                              ),\n                            ),\n                            Expanded(\n                              flex: 50,\n                              child: Column(\n                                children: <Widget>[\n                                  for(var numbers in rakipsayilariDuz)Text(numbers),\n                                ]\n                              ),\n                            ),\n                          ],\n                        ),\n                      ),\n                      Expanded(\n                        flex:10,\n                        child: Row(\n                          mainAxisAlignment: MainAxisAlignment.center,\n                          children: <Widget>[\n                            showDeleteNumbers(1,'numberimage1'),\n                            showDeleteNumbers(2,'numberimage2'),\n                            showDeleteNumbers(3,'numberimage3'),\n                            showDeleteNumbers(4,'numberimage4'),\n                            Expanded(\n                              child:FlatButton(\n                                onPressed: (){\n                                  sendnumber();\n                                },\n                                child: Image.asset('images/send.png'),\n                              ),\n                            ),\n                          ],\n                        ),\n                      ),\n                      Expanded(\n                        flex: 10,\n                        child: Row(\n                          children: <Widget>[\n                            attachNumber('1','one.png'),\n                            attachNumber('2','two.png'),\n                            attachNumber('3','three.png'),\n                            attachNumber('4','four.png'),\n                            attachNumber('5','five.png'),\n                            attachNumber('6','six.png'),\n                            attachNumber('7','seven.png'),\n                            attachNumber('8','eight.png'),\n                            attachNumber('9','nine.png'),\n                            attachNumber('0','zero.png'),\n                          ],\n                        ),\n                      ),\n                    ],\n                  ),\n                ),\n              );\n            }\n          },\n        );\n      }\n\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

小智 8

如果您不确定该小部件是否仍在构建,您始终可以使用以下解决方案:

SchedulerBinding.instance!.addPostFrameCallback((_) {
  //your dialog
});
Run Code Online (Sandbox Code Playgroud)


Far*_*ruk 2

经过一番研究后,我了解到问题的发生是因为函数小部件构建导致了问题,因为已经发生了构建事件,所以我创建了函数 asycn 并且它现在正在工作。

这是代码:

  _showMaterialDialog(String type) async{
    if(type=="win"){
      gameResult = "You Win, Gratz!";
    }else if(type=="lose"){
      gameResult = "You Lose :(";
    }
    print("buraya girdi");
    print(gameResult);
    await Future.delayed(Duration(milliseconds: 50));
    showDialog (
        context: context,
        builder: (_) => AlertDialog(
          title: Text("Result"),
          content: Text(gameResult),
          actions: <Widget>[
            FlatButton(
              child: Text('Close'),
              onPressed: () {
                  Navigator.pushNamed(context, HomeScreen.id);
              },
            )
          ],
        ));
  }
Run Code Online (Sandbox Code Playgroud)