dart 我在Flutter中尝试使用future时遇到问题

0qx6xfy6  于 2023-03-27  发布在  Flutter
关注(0)|答案(1)|浏览(164)

我是一个初学者,我正在努力学习Flutter。

ElevatedButton(
            child: Text(
                'Go to the new page'
            ),
            onPressed: () {
             Future<bool?> pushFuture = Navigator.of(context).push<bool>(
                  MaterialPageRoute(
                builder: (context) {
                  return VideoScreen('Do you like the video?');
                },
              )
              );
             pushFuture.then((bool? answer) async {
               print('answer is: ${answer}');
               if(answer == true) {
                 print("He/She liked!!!");
                 return Future.value(true);
               }else{
                 Navigator.of(context).push<bool>(
                     MaterialPageRoute(
                       builder: (context) {
                         return VideoScreen('I wish you would liked it... Do you like the video?');
                       },
                     )
                 );
               }
             }).then((value) {
               if(value == true){
                 print('You liked it!!!');
               }
             });

我有一个视频屏幕和两个答案按钮,分别是“是”和“否”。我想在他们单击“否”时再次提问,如果他们第二次单击“是”,我想在控制台屏幕上打印“您喜欢它!!!”。但它不起作用。你能帮助我吗?

3mpgtkmj

3mpgtkmj1#

试试这个格式

ElevatedButton(
  child: Text('Go to the new page'),
  onPressed: () async {
    bool? answer = await Navigator.of(context)
        .push<bool>(MaterialPageRoute(builder: (context) {
      return VideoScreen('Do you like the video?');
    }));

    if (answer == true) {
      print('You liked it!!!'); //same here
      print("He/She liked!!!");
      return Future.value(true);
    } else {
      final result = await Navigator.of(context)
          .push<bool>(MaterialPageRoute(builder: (context) {
        return VideoScreen(
            'I wish you would liked it... Do you like the video?');
      }));

      ///here another result
    }
  },
);

相关问题