text stringlengths 1 372 |
|---|
methodChannelWithName:@"samples.flutter.dev/battery" |
binaryMessenger:controller.binaryMessenger]; |
[batterychannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { |
// this method is invoked on the UI thread. |
// TODO |
}]; |
[generatedpluginregistrant registerWithRegistry:self]; |
return [super application:application didFinishLaunchingWithOptions:launchOptions]; |
} |
<code_end> |
next, add the iOS ObjectiveC code that uses the iOS battery APIs to |
retrieve the battery level. this code is exactly the same as you |
would write in a native iOS app. |
add the following method in the AppDelegate class, just before @end: |
<code_start> |
- (int)getbatterylevel { |
UIDevice* device = UIDevice.currentDevice; |
device.batteryMonitoringEnabled = YES; |
if (device.batterystate == UIDeviceBatteryStateUnknown) { |
return -1; |
} else { |
return (int)(device.batterylevel * 100); |
} |
} |
<code_end> |
finally, complete the setMethodCallHandler() method added earlier. |
you need to handle a single platform method, getBatteryLevel(), |
so test for that in the call argument. the implementation of |
this platform method calls the iOS code written in the previous step, |
and returns a response for both the success and error cases using |
the result argument. if an unknown method is called, report that instead. |
<code_start> |
__weak typeof(self) weakSelf = self; |
[batterychannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { |
// this method is invoked on the UI thread. |
if ([@"getbatterylevel" isEqualToString:call.method]) { |
int batteryLevel = [weakself getBatteryLevel]; |
if (batterylevel == -1) { |
result([FlutterError errorWithCode:@"UNAVAILABLE" |
message:@"Battery level not available." |
details:nil]); |
} else { |
result(@(batteryLevel)); |
} |
} else { |
result(FlutterMethodNotImplemented); |
} |
}]; |
<code_end> |
you should now be able to run the app on iOS. |
if using the iOS simulator, |
note that it doesn’t support battery APIs, |
and the app displays ‘battery level not available’. |
<topic_end> |
<topic_start> |
step 5: add a windows platform-specific implementation |
start by opening the windows host portion of your flutter app in visual studio: |
run flutter build windows in your project directory once to generate |
the visual studio solution file. |
start visual studio. |
select open a project or solution. |
navigate to the directory holding your flutter app, then into the build |
folder, then the windows folder, then select the batterylevel.sln file. |
click open. |
add the c++ implementation of the platform channel method: |
expand batterylevel > source files in the solution explorer. |
open the file flutter_window.cpp. |
first, add the necessary includes to the top of the file, just |
after #include "flutter_window.h": |
<code_start> |
#include <flutter/event_channel.h> |
#include <flutter/event_sink.h> |
#include <flutter/event_stream_handler_functions.h> |
#include <flutter/method_channel.h> |
#include <flutter/standard_method_codec.h> |
#include <windows.h> |
#include <memory> |
<code_end> |
edit the FlutterWindow::OnCreate method and create |
a flutter::MethodChannel tied to the channel name |
samples.flutter.dev/battery: |
<code_start> |
bool FlutterWindow::OnCreate() { |
// ... |
RegisterPlugins(flutter_controller_->engine()); |
flutter::MethodChannel<> channel( |
flutter_controller_->engine()->messenger(), "samples.flutter.dev/battery", |
&flutter::standardmethodcodec::getinstance()); |
channel.SetMethodCallHandler( |
[](const flutter::MethodCall<>& call, |
std::unique_ptr<flutter::MethodResult<>> result) { |
// TODO |
}); |
SetChildContent(flutter_controller_->view()->GetNativeWindow()); |
return true; |
} |
<code_end> |
next, add the c++ code that uses the windows battery APIs to |
retrieve the battery level. this code is exactly the same as |
you would write in a native windows application. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.