【问题标题】:Methods are not getting executed in the written order inside onPressed方法没有按照 onPressed 中的书面顺序执行
【发布时间】:2021-03-28 07:01:11
【问题描述】:

我正在尝试使用一个简单的凸起按钮创建一个颤振应用程序,该按钮执行以下操作:

  1. 使用短信包在后台发送短信打开网页 2.在应用程序中(仅5秒)使用url_launcher打开手机 3. 使用 onPressed 属性进行语音通话的原生应用。

我希望它按照这个顺序,以便我可以在最后打电话。但是,onPressed 内部会先打开本机电话应用程序,除非我退出电话应用程序,否则它不会让我的网页打开。

我很难理解为什么首先打开电话本机应用程序,即使我仅在进行 _launchInApp(toLaunch) 调用后才调用 _makePhoneCall() 方法。 sendSMS() 被正确调用

如何设置,只有在应用中打开网页并按照顺序调用电话本机应用?任何帮助都会很棒

下面是一段代码:

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    import 'package:sms/sms.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Packages testing',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Packages testing'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String _phone = '';
    
      _launchInApp(String url) async {
        if (await canLaunch(url)) {
          await launch(
            url,
            forceSafariVC: true,
            forceWebView: true,
            headers: <String, String>{'my_header_key': 'my_header_value'},
          );
        } else {
          throw 'Could not launch $url';
        }
      }
    
      _makePhoneCall(String url) async {
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    
      void sendSMS() {
        SmsSender sender = new SmsSender();
        sender.sendSms(new SmsMessage(_phone, 'Testing Handset'));
      }
    
      @override
      Widget build(BuildContext context) {
        const String toLaunch = 'https://flutter.dev/';
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: ListView(
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: TextField(
                        onChanged: (String text) => _phone = text,
                        decoration:
                            const InputDecoration(hintText: 'Phone Number')),
                  ),
                  FlatButton(
                    onPressed: () => setState(() {
                      sendSMS();
    
                      _launchInApp(toLaunch);
    
                      _makePhoneCall('tel:$_phone');
                    }),
                    child: const Text('Run All'),
                  ),
                  const Padding(padding: EdgeInsets.all(16.0)),
                ],
              ),
            ],
          ),
        );
      }
    }

【问题讨论】:

    标签: android flutter flutter-packages flutter-onpressed


    【解决方案1】:

    您必须在 _launchInApp 函数之前使用 await 关键字才能使其正常工作。试试下面的代码。

      FlatButton(
                    onPressed: () aync {
                      sendSMS();
    
                      await _launchInApp(toLaunch);
    
                      _makePhoneCall('tel:$_phone');
                    }),
                    child: const Text('Run All'),
                  ),
    

    【讨论】:

    • 感谢您的回复。我对代码进行了这些更改,但由于某种原因,我仍然看到电话本机应用程序首先打开FlatButton( onPressed: ()async{ sendSMS(); await _launchInApp(toLaunch); await _makePhoneCall('tel:$_phone'); }, child: const Text('Run All'), ),
    【解决方案2】:

    您创建了异步函数,但在调用它们时并未指定要等待它们完成。在 OnPressed 中添加 await 关键字

    【讨论】:

    • 感谢您的回复。我对代码进行了这些更改,但由于某种原因,我仍然看到电话本机应用程序首先打开FlatButton( onPressed: ()async{ sendSMS(); await _launchInApp(toLaunch); await _makePhoneCall('tel:$_phone'); }, child: const Text('Run All'), ),
    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多