Skip to content

Commit 34c63af

Browse files
authored
Update platform-services to new APIs (flutter#8488)
1 parent 1b52d46 commit 34c63af

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

examples/platform_services/lib/main.dart

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ class PlatformServices extends StatefulWidget {
1313
}
1414

1515
class _PlatformServicesState extends State<PlatformServices> {
16-
Future<dynamic> _locationRequest;
16+
static const PlatformMethodChannel platform = const PlatformMethodChannel('geo');
17+
String _location = 'Unknown location.';
18+
19+
Future<Null> _getLocation() async {
20+
List<double> result = await platform.invokeMethod('getLocation', 'network');
21+
22+
setState(() {
23+
_location = 'Latitude ${result[0]}, Longitude ${result[1]}.';
24+
});
25+
}
1726

1827
@override
1928
Widget build(BuildContext context) {
@@ -22,45 +31,16 @@ class _PlatformServicesState extends State<PlatformServices> {
2231
child: new Column(
2332
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
2433
children: <Widget>[
25-
new Text('Hello from Flutter!'),
2634
new RaisedButton(
2735
child: new Text('Get Location'),
28-
onPressed: _requestLocation,
29-
),
30-
new FutureBuilder<dynamic>(
31-
future: _locationRequest,
32-
builder: _buildLocation,
36+
onPressed: _getLocation,
3337
),
38+
new Text(_location)
3439
],
3540
),
3641
),
3742
);
3843
}
39-
40-
void _requestLocation() {
41-
setState(() {
42-
_locationRequest = const PlatformMethodChannel('geo').invokeMethod(
43-
'getLocation',
44-
'network',
45-
);
46-
});
47-
}
48-
49-
Widget _buildLocation(BuildContext context, AsyncSnapshot<dynamic> snapshot) {
50-
switch (snapshot.connectionState) {
51-
case ConnectionState.none:
52-
return new Text('Press button to request location');
53-
case ConnectionState.waiting:
54-
return new Text('Awaiting response...');
55-
default:
56-
try {
57-
final List<double> location = snapshot.requireData;
58-
return new Text('Lat. ${location[0]}, Long. ${location[1]}');
59-
} on PlatformException catch (e) {
60-
return new Text('Request failed: ${e.message}');
61-
}
62-
}
63-
}
6444
}
6545

6646
void main() {

0 commit comments

Comments
 (0)