Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9686d66

Browse files
committed
Fix silly use of json.encode
1 parent 5964467 commit 9686d66

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

lib/web_ui/lib/src/engine/keyboard.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class Keyboard {
131131
if (data == null) {
132132
return;
133133
}
134-
final String response = utf8.decode(data.buffer.asUint8List());
135-
final Map<String, dynamic> jsonResponse = json.decode(response);
134+
final Map<String, dynamic> jsonResponse = _messageCodec.decodeMessage(data);
136135
if (jsonResponse['handled'] as bool) {
137136
// If the framework handled it, then don't propagate it any further.
138137
event.preventDefault();

lib/web_ui/test/keyboard_test.dart

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import 'dart:html' as html;
77
import 'dart:js_util' as js_util;
88
import 'dart:typed_data';
9-
import 'dart:convert' hide Codec;
109

1110
import 'package:quiver/testing/async.dart';
1211
import 'package:test/bootstrap/browser.dart';
@@ -18,14 +17,6 @@ void main() {
1817
internalBootstrapBrowserTest(() => testMain);
1918
}
2019

21-
ByteData _toByteData(List<int> bytes) {
22-
final ByteData byteData = ByteData(bytes.length);
23-
for (int i = 0; i < bytes.length; i++) {
24-
byteData.setUint8(i, bytes[i]);
25-
}
26-
return byteData;
27-
}
28-
2920
void testMain() {
3021
group('Keyboard', () {
3122
/// Used to save and restore [ui.window.onPlatformMessage] after each test.
@@ -236,21 +227,13 @@ void testMain() {
236227
});
237228

238229
test('prevents default when key is handled by the framework', () {
239-
ByteData _toByteData(List<int> bytes) {
240-
final ByteData byteData = ByteData(bytes.length);
241-
for (int i = 0; i < bytes.length; i++) {
242-
byteData.setUint8(i, bytes[i]);
243-
}
244-
return byteData;
245-
}
246-
247230
Keyboard.initialize();
248231

249232
int count = 0;
250233
ui.window.onPlatformMessage = (String channel, ByteData data,
251234
ui.PlatformMessageResponseCallback callback) {
252235
count += 1;
253-
ByteData response = _toByteData(utf8.encode(json.encode(<String, dynamic>{'handled': true})));
236+
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': true});
254237
callback(response);
255238
};
256239

@@ -273,7 +256,7 @@ void testMain() {
273256
ui.window.onPlatformMessage = (String channel, ByteData data,
274257
ui.PlatformMessageResponseCallback callback) {
275258
count += 1;
276-
ByteData response = _toByteData(utf8.encode(json.encode(<String, dynamic>{'handled': false})));
259+
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': false});
277260
callback(response);
278261
};
279262

@@ -320,7 +303,7 @@ void testMain() {
320303
ui.window.onPlatformMessage = (String channel, ByteData data,
321304
ui.PlatformMessageResponseCallback callback) {
322305
count += 1;
323-
ByteData response = _toByteData(utf8.encode(json.encode(<String, dynamic>{'handled': true})));
306+
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': true});
324307
callback(response);
325308
};
326309

0 commit comments

Comments
 (0)