how to get a short code sms number in flutter code example

Example 1: how to get a short code sms number in flutter

class MyHomePage extends StatefulWidget {
  
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _nirbinumber = '12345';
  // _lastMessage is probably redundant, as you can use msg (below)
  SmsMessage _lastMessage = new SmsMessage('', '');

  
  void initState() {
    super.initState();
    // listen to the stream of *all* message arriving
    new SmsReceiver().onSmsReceived.listen((SmsMessage msg) {
      // filter out the replies by number
      if (msg.address == _nirbinumber) {
        // fantastique - it's one of the ones we want
        setState(() {
          _lastMessage = msg;
        });
        saveGeoValue(msg); // this cannot use the value in _lastMessage as it will not have been set yet
        showMap(msg);
      }
    });
  }

  void _send() {
    // fire (and forget)
    new SmsSender().sendSms(new SmsMessage(_nirbinumber, 'test message'));
  }

  
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('SMS demo'),
      ),
      body: new Center(
        child: new Text(_lastMessage.body),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _send,
        tooltip: 'Send SMS',
        child: new Icon(Icons.sms),
      ),
    );
  }
}

Example 2: how to get a short code sms number in flutter


  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Container(
                height: 250.0,
                child: new Stack(
                  children: <Widget>[
                    new Center(
                      child :
                       new Container(
                        child : new FloatingActionButton(
                            child : new Icon (Icons.location_on),

                            onPressed: ()
                            async {

                               SmsMessage message = new SmsMessage(_nirbinumber, '$loc1');    
                               sender.sendSms(message);
                               message.addStateListener((state) {
                               if (state == SmsMessageState.Delivered) {
                                 receiver.onSmsReceived.listen((SmsMessage msg) => _lastMessage = msg);

                                  setState(() {
                                    savegeoValue();    // function to extract GPS coordinate of the received sms and save it with "shared preference" 
                                    StreamSubscription<SmsMessage> _smsSubscription;
                                    }
                                  );
                                  showMap();   // function to launch googlemap view
                                 }
                                }
                              );
                            }
                        ),
                      ),
                    ),
                  ],
                )
            )
          ],
        )
    )
);

Tags:

Dart Example