Flutter & Dart Development

The Ultimate Guide to dart_simple_live Setup for 2025

Ready to build live stream apps in Dart? Our 2025 guide to dart_simple_live covers setup, advanced features, and Flutter integration for Bilibili, Douyu & more.

A

Alexandre Dubois

Senior Flutter developer and open-source contributor specializing in real-time data applications.

8 min read11 views

Ever wanted to tap into the vibrant world of live streaming platforms like Bilibili, Douyu, or Huya directly from your Dart or Flutter application? As we head into 2025, the demand for integrated, real-time experiences is higher than ever. That's where dart_simple_live comes in, a powerhouse library that simplifies this entire process. Forget wrestling with complex WebSocket protocols and cryptic data formats; this guide will get you set up and streaming data in minutes.

What Exactly Is dart_simple_live?

dart_simple_live is a pure Dart library designed to provide a unified, simple interface for interacting with the live streaming services of major platforms. At its core, it connects to a platform's real-time messaging server (usually via WebSockets), decodes the binary data stream, and emits structured, easy-to-use Dart objects for events like:

  • Live Comments (Danmaku): The iconic scrolling comments.
  • Gifts: When a viewer sends a virtual gift to the streamer.
  • Super Chats: Highlighted, paid messages.
  • Room Updates: Changes in viewer count, stream title, or online/offline status.

It effectively acts as a bridge, abstracting away the platform-specific complexities and giving you a clean, event-driven API to build upon. Whether you're creating a custom viewing client, a data analysis tool, or a bot, this library is your perfect starting point.

Why It's the Go-To Choice for 2025

In 2025, developer efficiency is paramount. While you could spend weeks reverse-engineering a platform's API, dart_simple_live offers immediate value. The library has matured significantly, boasting improved stability and a growing community. Its main advantages are:

  • Performance: Being a native Dart solution, it's incredibly lightweight and fast, making it ideal for performance-critical Flutter apps.
  • Simplicity: The unified API means you can learn it once and apply it to multiple platforms with minimal code changes.
  • Actively Maintained: Live streaming platforms frequently change their APIs. An active library means you're less likely to be left with broken code after a platform update.
  • Type Safety: It provides strongly-typed event objects, which reduces runtime errors and improves developer experience with autocompletion.

Getting Started: The Core Setup

Let's get our hands dirty. You'll be surprised how quickly you can get this running.

Prerequisites

Advertisement
  • A recent Dart SDK (or a Flutter installation).
  • A code editor like VS Code or Android Studio.
  • A valid Room ID from a supported platform (e.g., a Bilibili live room ID).

Installation

Add the library to your project by running this command in your terminal:

dart pub add dart_simple_live

Or, add it manually to your pubspec.yaml file:

dependencies:
  dart_simple_live: ^latest

Then run dart pub get or flutter pub get.

Your First Connection: A Basic Example

Let's create a simple command-line application that connects to a Bilibili live room and prints incoming comments (danmaku).

import 'package:dart_simple_live/dart_simple_live.dart';

void main() async {
  // 1. Get the Bilibili site instance
  final bilibili = SimpleLive.of(Sites.bilibili);

  // 2. Listen for events from the library
  bilibili.onEvent.listen((event) {
    // Check for specific event types
    if (event is LiveDanmaku) {
      print('[Comment] ${event.userName}: ${event.message}');
    } else if (event is LiveGift) {
      print('[Gift] ${event.userName} sent a ${event.giftName}!');
    } else if (event is LiveConnected) {
      print('Successfully connected to the room!');
    }
  });

  // 3. Connect to a specific room ID
  // Replace '22637215' with any Bilibili live room ID
  await bilibili.connect('22637215');

  print('Listening for live events... Press Ctrl+C to exit.');
}

Run this Dart file, and you'll see live comments from the specified room stream into your console in real-time. It's that easy!

dart_simple_live vs. Other Methods

To understand its value, let's compare dart_simple_live with alternative approaches.

Feature dart_simple_live Custom WebSocket Client Third-Party Web Service
Ease of Setup High (minutes) Very Low (days or weeks) Medium (hours)
Platform Support Built-in for Bilibili, Douyu, etc. Manual implementation per platform Depends on the service provider
Maintenance Low (rely on library updates) High (track all API changes yourself) Low (rely on service provider)
Performance Excellent (native Dart, direct connection) Excellent (native Dart, direct connection) Good (adds network latency)
Cost Free (open source) Free (development time is the cost) Often has subscription fees

Diving Deeper: Advanced Features

Once you've mastered the basics, you can unlock more powerful functionality.

Handling Multiple Platforms

The library's design makes it trivial to manage multiple connections simultaneously. You just need to get an instance for each site.

// Get instances for different platforms
final bilibili = SimpleLive.of(Sites.bilibili);
final douyu = SimpleLive.of(Sites.douyu);

// Connect to rooms on each platform
await bilibili.connect('22637215');
await douyu.connect('9999');

// You can listen to their onEvent streams independently
bilibili.onEvent.listen(handleBilibiliEvents);
douyu.onEvent.listen(handleDouyuEvents);

Mastering Event Types

The real power comes from handling the rich variety of events. A robust handler might look like this:

void handleLiveEvents(LiveEvent event) {
  switch (event.runtimeType) {
    case LiveDanmaku:
      final danmaku = event as LiveDanmaku;
      // Logic to display the comment
      break;
    case LiveGift:
      final gift = event as LiveGift;
      // Logic to show a gift animation
      break;
    case LiveSuperChat:
      final superChat = event as LiveSuperChat;
      // Logic to display a highlighted message
      break;
    case LiveRoomInfo:
      final info = event as LiveRoomInfo;
      // Logic to update viewer count or stream title
      print('Viewers: ${info.popular}');
      break;
    default:
      // You can log other events for debugging
      print('Received unhandled event: ${event.runtimeType}');
  }
}

Configuration and Customization

For certain actions, like sending comments or accessing user-specific data, you'll need to provide authentication cookies. The library allows you to set these easily.

final bilibili = SimpleLive.of(Sites.bilibili);

// Set cookies before connecting
bilibili.setCookie('your_bilibili_cookie_string_here');

await bilibili.connect('your_room_id');

You can also configure other options, such as custom headers or WebSocket parameters, through the library's configuration objects for more advanced use cases.

Common Pitfalls and Troubleshooting

  • Connection Failed: This is often due to an incorrect room ID, network issues, or a temporary outage on the platform's side. Double-check your room ID and internet connection.
  • Incorrect Room ID Format: Some platforms use short, vanity IDs in their URLs, but the API requires the original, longer numeric ID. Always try to find the original ID.
  • Events Stop Coming In: Live streaming platforms sometimes change their API or data format. The first step is to update dart_simple_live to the latest version: dart pub upgrade dart_simple_live. If the issue persists, check the library's GitHub issues for recent reports.

Putting It All Together: A Simple Flutter App

Integrating with Flutter is seamless. You can use a StreamBuilder to automatically update your UI as new events arrive.

Here’s a conceptual example of a widget that displays a live feed of comments:

import 'package:flutter/material.dart';
import 'package:dart_simple_live/dart_simple_live.dart';

class LiveCommentFeed extends StatefulWidget {
  final String roomId;
  const LiveCommentFeed({Key? key, required this.roomId}) : super(key: key);

  @override
  _LiveCommentFeedState createState() => _LiveCommentFeedState();
}

class _LiveCommentFeedState extends State {
  final Bilibili _bilibili = SimpleLive.of(Sites.bilibili);
  final List _danmakuList = [];

  @override
  void initState() {
    super.initState();
    _connectToRoom();
  }

  void _connectToRoom() async {
    _bilibili.onEvent.listen((event) {
      if (event is LiveDanmaku) {
        setState(() {
          _danmakuList.insert(0, event); // Add to top of the list
        });
      }
    });
    await _bilibili.connect(widget.roomId);
  }

  @override
  void dispose() {
    _bilibili.disconnect(); // Clean up the connection
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      reverse: true, // To show newest comments at the bottom
      itemCount: _danmakuList.length,
      itemBuilder: (context, index) {
        final danmaku = _danmakuList[index];
        return ListTile(
          title: Text(danmaku.message),
          subtitle: Text(danmaku.userName),
        );
      },
    );
  }
}

This widget initializes the connection, listens for danmaku events, and updates its state to rebuild the list, creating a real-time comment feed. Remember to handle connection cleanup in the dispose method!

Final Thoughts

As of 2025, dart_simple_live stands out as the most efficient and developer-friendly way to integrate live streaming data into your Dart and Flutter projects. By handling the messy, platform-specific details, it frees you to focus on what truly matters: building innovative and engaging user experiences. The barrier to entry has never been lower. So, grab a room ID, start coding, and bring the dynamic energy of live streams into your next application!

You May Also Like