这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix crash in Linux platform channel example. #155735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions examples/platform_channel/linux/my_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions examples/platform_channel/test/platform_channel_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}