blob: bded8afebb6b6b86121594c9fb47d84c199a4942 [file] [log] [blame] [view]
# connectToService<T> method
*[<Null safety>](https://dart.dev/null-safety)*
void connectToService
&lt;T>([AsyncProxy](../../package-fidl_fidl/AsyncProxy-class.md)&lt;T>? serviceProxy, {String? name})
<p>Connects to the incoming service specified by <code>serviceProxy</code>.</p>
<p>If this object is not bound via the <a href="../../package-fuchsia_services_services/Incoming/request.md">request</a> method before
this method is called an <a href="../../package-fuchsia_services_services/IncomingStateException-class.md">IncomingStateException</a> will be thrown.</p>
<p>If <code>name</code> is specified, it is used as the service name to connect to,
instead of the default name provided by the proxy.</p>
## Implementation
```dart
void connectToService<T>(AsyncProxy<T>? serviceProxy, {String? name}) {
if (serviceProxy == null) {
throw ArgumentError.notNull('serviceProxy');
}
final String? serviceName = name ?? serviceProxy.ctrl.$serviceName;
if (serviceName == null) {
throw Exception(
"${serviceProxy.ctrl.$interfaceName}'s controller.\$serviceName must "
'not be null. Check the FIDL file for a missing [Discoverable]');
}
// Creates an interface request and binds one of the channels. Binding this
// channel prior to connecting to the agent allows the developer to make
// proxy calls without awaiting for the connection to actually establish.
final serviceProxyRequest = serviceProxy.ctrl.request();
connectToServiceByNameWithChannel(
serviceName, serviceProxyRequest.passChannel());
}
```