Home > Mobile >  How to change data using platform chanel with Swift?
How to change data using platform chanel with Swift?

Time:06-21

I have profile data in the form of a Map. I need to pass them to the platform and return them back with a modified update date using the platform channel. How can I change the update date and revert back? The data should be updated on clicking the ElevatedButton. My Flutter Page:

static const MethodChannel _platform =
    MethodChannel('samples.flutter.dev/profile');

Future<PlatformChannelState> _getUpdateProfile(ChangeProfile event) async {
  Map<String, String> newProfile = event.newProfile;
  try {
    final String result = await _platform.invokeMethod('updateProfile');
    newProfile = {
      'name': 'Name',
      'date of birth': 'Date',
      'profile update date': result,
    };
  } on PlatformException catch (e) {
    newProfile = {
      'name': 'Name',
      'date of birth': 'Date',
      'profile update date': '${e.message}',
    };
  }
  return GotProfileUpdate(newProfile);
}

@UIApplicationMain

@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool { 

      let profileChannel = FlutterMethodChannel(name: "samples.flutter.dev/profile",binaryMessenger: controller.binaryMessenger)
      profileChannel.setMethodCallHandler({
    (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in 

    })

      GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)

  }

}

CodePudding user response:

I am not entirely sure why the time has to come from the native side and not just simply use Dart's DateTime class.

However to do it over the method channel you could generate an integer representing the time in seconds (or milliseconds) since 1970 (also called Unix Epoch) and send the value back to Dart:

@UIApplicationMain

@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool { 

      let profileChannel = FlutterMethodChannel(name: "samples.flutter.dev/profile",binaryMessenger: controller.binaryMessenger)
      profileChannel.setMethodCallHandler({
    (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in 
      // Example of generating timestamp in seconds (multiply by 1000 for milliseconds
      let timestamp = Date().timeIntervalSince1970
      result(Int(timestamp))
    })

      GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)

  }

}

On the Dart side you can of course use this result directly as an int or convert it to a DateTime value like so:

var dateTime = DateTime.fromMillisecondsSinceEpoch(result * 1000);
  • Related