Fix crash in Linux platform channel example. (#155735)

When running this example it was crashing, so investigated a fixed a
couple of bugs.
This commit is contained in:
Robert Ancell 2024-10-01 13:09:49 +13:00 committed by GitHub
parent bff135153d
commit cf4dace52b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 9 deletions

View File

@ -42,8 +42,7 @@ static void update_charging_state(MyApplication* self) {
const gchar* charge_event = "discharging";
for (guint i = 0; i < self->battery_devices->len; i++) {
UpDevice* device =
static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i));
UpDevice* device = UP_DEVICE(g_ptr_array_index(self->battery_devices, i));
guint state;
g_object_get(device, "state", &state, nullptr);
@ -97,8 +96,7 @@ static void up_device_removed_cb(MyApplication* self, UpDevice* device) {
static FlMethodResponse* get_battery_level(MyApplication* self) {
// Find the first available battery and use that.
for (guint i = 0; i < self->battery_devices->len; i++) {
UpDevice* device =
static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i));
UpDevice* device = UP_DEVICE(g_ptr_array_index(self->battery_devices, i));
double percentage;
g_object_get(device, "percentage", &percentage, nullptr);
@ -225,8 +223,7 @@ static void my_application_activate(GApplication* application) {
g_autoptr(GPtrArray) devices = up_client_get_devices(self->up_client);
#endif
for (guint i = 0; i < devices->len; i++) {
g_autoptr(UpDevice) device =
static_cast<UpDevice*>(g_ptr_array_index(devices, i));
UpDevice* device = UP_DEVICE(g_ptr_array_index(devices, i));
up_device_added_cb(self, device);
}
@ -268,8 +265,7 @@ static void my_application_dispose(GObject* object) {
MyApplication* self = MY_APPLICATION(object);
for (guint i = 0; i < self->battery_devices->len; i++) {
UpDevice* device =
static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i));
UpDevice* device = UP_DEVICE(g_ptr_array_index(self->battery_devices, i));
g_signal_handlers_disconnect_matched(device, G_SIGNAL_MATCH_DATA, 0, 0,
nullptr, nullptr, self);
}
@ -278,7 +274,7 @@ static void my_application_dispose(GObject* object) {
g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
g_clear_object(&self->up_client);
g_clear_object(&self->battery_devices);
g_clear_pointer(&self->battery_devices, g_ptr_array_unref);
g_clear_object(&self->battery_channel);
g_clear_object(&self->charging_channel);
g_clear_pointer(&self->last_charge_event, g_free);

View File

@ -0,0 +1,15 @@
// Copyright 2014 The Flutter 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 'package:flutter_test/flutter_test.dart';
import 'package:platform_channel/main.dart' as platform_channel;
void main() {
testWidgets('Platform channel smoke test', (WidgetTester tester) async {
platform_channel.main(); // builds the app and schedules a frame but doesn't trigger one
await tester.pump(); // triggers a frame
expect(find.textContaining('Battery level: '), findsOneWidget);
});
}