diff --git a/examples/platform_services/lib/main.dart b/examples/platform_services/lib/main.dart index 8a150dc5e0f..aeaf795969d 100644 --- a/examples/platform_services/lib/main.dart +++ b/examples/platform_services/lib/main.dart @@ -13,7 +13,16 @@ class PlatformServices extends StatefulWidget { } class _PlatformServicesState extends State { - Future _locationRequest; + static const PlatformMethodChannel platform = const PlatformMethodChannel('geo'); + String _location = 'Unknown location.'; + + Future _getLocation() async { + List result = await platform.invokeMethod('getLocation', 'network'); + + setState(() { + _location = 'Latitude ${result[0]}, Longitude ${result[1]}.'; + }); + } @override Widget build(BuildContext context) { @@ -22,45 +31,16 @@ class _PlatformServicesState extends State { child: new Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - new Text('Hello from Flutter!'), new RaisedButton( child: new Text('Get Location'), - onPressed: _requestLocation, - ), - new FutureBuilder( - future: _locationRequest, - builder: _buildLocation, + onPressed: _getLocation, ), + new Text(_location) ], ), ), ); } - - void _requestLocation() { - setState(() { - _locationRequest = const PlatformMethodChannel('geo').invokeMethod( - 'getLocation', - 'network', - ); - }); - } - - Widget _buildLocation(BuildContext context, AsyncSnapshot snapshot) { - switch (snapshot.connectionState) { - case ConnectionState.none: - return new Text('Press button to request location'); - case ConnectionState.waiting: - return new Text('Awaiting response...'); - default: - try { - final List location = snapshot.requireData; - return new Text('Lat. ${location[0]}, Long. ${location[1]}'); - } on PlatformException catch (e) { - return new Text('Request failed: ${e.message}'); - } - } - } } void main() {