我有一个对象列表,将在本地保存为.json文件。问题是我不想在用户每次修改列表对象时更新.json文件。任何窗口焦点更改都足以触发save()函数,以确保数据不会丢失。如何实现此场景或任何其他建议?
rhfm7lfc1#
Android侧
class MainActivity : FlutterFragmentActivity(), EventChannel.StreamHandler { private var eventSink: EventChannel.EventSink? = null override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) startEventChannel(flutterEngine.dartExecutor.binaryMessenger) } private fun startEventChannel(messenger: BinaryMessenger) { EventChannel(messenger, "Channel").setStreamHandler(this) } override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) eventSink?.success(hasFocus) } override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { eventSink = events } override fun onCancel(arguments: Any?) { eventSink = null } }
Flutter侧
final _eventChannel = const EventChannel('Channel'); Future<void> getValue() async { try { _eventChannel .receiveBroadcastStream() .asyncMap((dynamic event) => event) .listen((event) { emit(state.copyWith( status: Status.success, hasFocus: event as bool, )); }); } on PlatformException catch (e) { emit(state.copyWith( status: Status.failure, errorMessage: e.message ?? "Unknown error", )); } }
1条答案
按热度按时间rhfm7lfc1#
Android侧
Flutter侧