Search packages, repositories and app categories

Package detailsv2.1.0

cast

Dart package to discover and connect with Chromecast devices

30-day downloads
16.5K
Likes
94
Pub points
80

Install package

flutter pub add cast

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The cast package is a Dart library designed to enable discovery and connection with Chromecast devices in Flutter applications. It allows developers to stream audio, video, and other content from their apps to compatible Chromecast-enabled displays. The package supports seamless device detection and media control, making it ideal for building media-centric apps that integrate with smart TVs and streaming hardware. Built with simplicity in mind, it provides a straightforward API for casting functionality without requiring deep knowledge of the underlying protocols. It is maintained by eivo.co and available on GitHub for community contributions.

Use cases

  • Streaming media to Chromecast devices
  • Building TV or home entertainment apps
  • Enabling remote playback control
  • Integrating with smart home ecosystems
  • Creating cross-device media experiences

Key features

  • Automatic Chromecast device discovery
  • Support for audio and video casting
  • Simple, intuitive API
  • Cross-platform compatibility (with platform-specific implementation)
  • Open-source and community-maintained

Suitable for

  • Developers building media apps
  • Apps targeting smart TVs or streaming devices
  • Projects requiring device-to-device casting
  • Teams working on IoT or home automation integrations
  • Flutter apps aiming for multi-screen experiences

Considerations

  • Requires Android and iOS platform setup for full functionality
  • Limited to Chromecast-compatible devices
  • May require additional configuration for complex media streams
  • Depends on external network connectivity
  • Not suitable for non-media casting use cases

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Pub Likes Pub Popularity Pub Points

cast

Dart package to discover and connect with Chromecast devices

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  cast: ^2.1.0
iOS

Required iOS 13 minimum. Since iOS 14 a few more steps are required https://developers.google.com/cast/docs/ios_sender/ios_permissions_changes

In ios/Runner/Info.plist add:

<key>NSBonjourServices</key>
<array>
  <string>_googlecast._tcp</string>
  <string>_YOUR_APP_ID._googlecast._tcp</string>
</array>

<key>NSLocalNetworkUsageDescription</key>
<string>${PRODUCT_NAME} uses the local network to discover Cast-enabled devices on your WiFi
network.</string>

Usage

List devices:

Widget build(BuildContext context) {
  return FutureBuilder<List<CastDevice>>(
    future: CastDiscoveryService().search(),
    builder: (context, snapshot) {
      if (snapshot.hasError) {
        return Center(
          child: Text(
            'Error: ${snapshot.error.toString()}',
          ),
        );
      } else if (!snapshot.hasData) {
        return Center(
          child: CircularProgressIndicator(),
        );
      }

      if (snapshot.data!.isEmpty) {
        return Column(
          children: [
            Center(
              child: Text(
                'No Chromecast founded',
              ),
            ),
          ],
        );
      }

      return Column(
        children: snapshot.data!.map((device) {
          return ListTile(
            title: Text(device.name),
            onTap: () {
              _connectToYourApp(context, device);
            },
          );
        }).toList(),
      );
    },
  );
}

Connect to device:

Future<void> _connect(BuildContext context, CastDevice object) async {
  final session = await CastSessionManager().startSession(object);

  session.stateStream.listen((state) {
    if (state == CastSessionState.connected) {
      _sendMessage(session);
    }
  });

  session.messageStream.listen((message) {
    print('receive message: $message');
  });

  session.sendMessage(CastSession.kNamespaceReceiver, {
    'type': 'LAUNCH',
    'appId': 'YouTube', // set the appId of your app here
  });
}

CastSessionManager is used to keep track of all sessions.

Send message:

void _sendMessage(CastSession session) {
  session.sendMessage('urn:x-cast:namespace-of-the-app', {
    'type': 'sample',
  });
}

Except for the launch message, you should wait until the session have a connected state before sending message.

Note

Some informations about the protocol used https://docs.rs/crate/gcast/0.1.5/source/PROTOCOL.md

$ pub global activate protoc_plugin
$ export PATH="$PATH":"$HOME/.pub-cache/bin"
$ protoc -I=./lib/cast_channel --dart_out=./lib/cast_channel ./lib/cast_channel/cast_channel.proto --plugin "pub run protoc_plugin"

Author

Categories

Dependencies

Dependency list 2 items

Source repository

Projects using this package