我有地圖形式的個人資料資料。我需要將它們傳遞給平臺并使用平臺通道將它們回傳并修改更新日期。如何更改更新日期并恢復?單擊 ElevatedButton 時應更新資料。我的顫振頁面:
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)
}
}
uj5u.com熱心網友回復:
我不完全確定為什么時間必須來自本地,而不僅僅是簡單地使用 Dart 的DateTime類。
但是,要通過方法通道執行此操作,您可以生成一個整數,表示自 1970 年(也稱為 Unix 紀元)以來的時間(以秒(或毫秒)為單位)并將值發送回 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)
}
}
在 Dart 方面,您當然可以直接將此結果用作 int 或將其轉換為DateTime如下值:
var dateTime = DateTime.fromMillisecondsSinceEpoch(result * 1000);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494323.html
上一篇:快速創建按鈕時出現回圈錯誤
下一篇:如何用這個json制作結構?
