Fixed bug where we could accidently call a callback twice. (#43467)
This commit is contained in:
parent
05c3f7b6a1
commit
53dcf92f7d
@ -200,6 +200,7 @@ class _DefaultBinaryMessenger extends BinaryMessenger {
|
|||||||
response = await handler(data);
|
response = await handler(data);
|
||||||
} else {
|
} else {
|
||||||
ui.channelBuffers.push(channel, data, callback);
|
ui.channelBuffers.push(channel, data, callback);
|
||||||
|
callback = null;
|
||||||
}
|
}
|
||||||
} catch (exception, stack) {
|
} catch (exception, stack) {
|
||||||
FlutterError.reportError(FlutterErrorDetails(
|
FlutterError.reportError(FlutterErrorDetails(
|
||||||
@ -209,9 +210,11 @@ class _DefaultBinaryMessenger extends BinaryMessenger {
|
|||||||
context: ErrorDescription('during a platform message callback'),
|
context: ErrorDescription('during a platform message callback'),
|
||||||
));
|
));
|
||||||
} finally {
|
} finally {
|
||||||
|
if (callback != null) {
|
||||||
callback(response);
|
callback(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<ByteData> send(String channel, ByteData message) {
|
Future<ByteData> send(String channel, ByteData message) {
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
import 'dart:ui' as ui;
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
ByteData _makeByteData(String str) {
|
||||||
|
final List<int> list = utf8.encode(str);
|
||||||
|
final ByteBuffer buffer =
|
||||||
|
list is Uint8List ? list.buffer : Uint8List.fromList(list).buffer;
|
||||||
|
return ByteData.view(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
test('default binary messenger calls callback once', () async {
|
||||||
|
int count = 0;
|
||||||
|
const String channel = 'foo';
|
||||||
|
ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
|
||||||
|
channel, _makeByteData('bar'), (ByteData message) async {
|
||||||
|
count += 1;
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
expect(count, equals(0));
|
||||||
|
await ui.channelBuffers.drain(channel,
|
||||||
|
(ByteData data, ui.PlatformMessageResponseCallback callback) {
|
||||||
|
callback(null);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
expect(count, equals(1));
|
||||||
|
});
|
||||||
|
}
|
@ -82,6 +82,7 @@ class _PlatformBinaryMessenger extends BinaryMessenger {
|
|||||||
response = await handler(data);
|
response = await handler(data);
|
||||||
} else {
|
} else {
|
||||||
ui.channelBuffers.push(channel, data, callback);
|
ui.channelBuffers.push(channel, data, callback);
|
||||||
|
callback = null;
|
||||||
}
|
}
|
||||||
} catch (exception, stack) {
|
} catch (exception, stack) {
|
||||||
FlutterError.reportError(FlutterErrorDetails(
|
FlutterError.reportError(FlutterErrorDetails(
|
||||||
@ -91,9 +92,11 @@ class _PlatformBinaryMessenger extends BinaryMessenger {
|
|||||||
context: ErrorDescription('during a plugin platform message call'),
|
context: ErrorDescription('during a plugin platform message call'),
|
||||||
));
|
));
|
||||||
} finally {
|
} finally {
|
||||||
|
if (callback != null) {
|
||||||
callback(response);
|
callback(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Sends a platform message from the platform side back to the framework.
|
/// Sends a platform message from the platform side back to the framework.
|
||||||
@override
|
@override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user