Polish HostMessages (#4515)
Cleans up a few style nits in hello_services and adds support for automatic JSON encoding and decoding to the HostMessages interface.
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
@property(strong, nonatomic) UIWindow *window;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -14,13 +14,13 @@
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||
FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration];
|
||||
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
|
||||
FlutterViewController* controller = [[FlutterViewController alloc] initWithProject:project
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
_locationProvider = [[LocationProvider alloc] init];
|
||||
[controller addMessageListener: _locationProvider];
|
||||
[flutterController addMessageListener:_locationProvider];
|
||||
|
||||
self.window.rootViewController = controller;
|
||||
self.window.rootViewController = flutterController;
|
||||
[self.window makeKeyAndVisible];
|
||||
return YES;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -11,15 +10,13 @@ import 'package:flutter/services.dart';
|
||||
|
||||
final Random random = new Random();
|
||||
|
||||
Future<String> handleGetRandom(String json) async {
|
||||
Map<String, dynamic> message = JSON.decode(json);
|
||||
double min = message['min'].toDouble();
|
||||
double max = message['max'].toDouble();
|
||||
Future<dynamic> handleGetRandom(Map<String, dynamic> message) async {
|
||||
final double min = message['min'].toDouble();
|
||||
final double max = message['max'].toDouble();
|
||||
|
||||
double value = (random.nextDouble() * (max - min)) + min;
|
||||
|
||||
Map<String, double> reply = <String, double>{'value': value};
|
||||
return JSON.encode(reply);
|
||||
return <String, double>{
|
||||
'value': (random.nextDouble() * (max - min)) + min
|
||||
};
|
||||
}
|
||||
|
||||
class HelloServices extends StatefulWidget {
|
||||
@@ -50,14 +47,14 @@ class _HelloServicesState extends State<HelloServices> {
|
||||
);
|
||||
}
|
||||
|
||||
void _getLocation() {
|
||||
Map<String, String> message = <String, String>{'provider': 'network'};
|
||||
HostMessages.sendToHost('getLocation', JSON.encode(message))
|
||||
.then(_onReceivedLocation);
|
||||
}
|
||||
|
||||
void _onReceivedLocation(String json) {
|
||||
Map<String, num> reply = JSON.decode(json);
|
||||
Future<Null> _getLocation() async {
|
||||
final Map<String, String> message = <String, String>{'provider': 'network'};
|
||||
final Map<String, dynamic> reply = await HostMessages.sendJSON('getLocation', message);
|
||||
// If the widget was removed from the tree while the message was in flight,
|
||||
// we want to discard the reply rather than calling setState to update our
|
||||
// non-existant appearance.
|
||||
if (!mounted)
|
||||
return;
|
||||
setState(() {
|
||||
_latitude = reply['latitude'].toDouble();
|
||||
_longitude = reply['longitude'].toDouble();
|
||||
@@ -68,5 +65,5 @@ class _HelloServicesState extends State<HelloServices> {
|
||||
void main() {
|
||||
runApp(new HelloServices());
|
||||
|
||||
HostMessages.addMessageHandler('getRandom', handleGetRandom);
|
||||
HostMessages.addJSONMessageHandler('getRandom', handleGetRandom);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user