isStreamingSupported static method

Future<bool> isStreamingSupported()

Check if OPFS streaming mode is supported by the current browser

Returns true on web if OPFS is available, false otherwise. Always returns false on mobile platforms.

OPFS (Origin Private File System) is required for streaming mode, which allows loading large models (>2GB) without hitting ArrayBuffer limits.

Browser support:

  • Chrome 86+ ✅
  • Edge 86+ ✅
  • Safari 15.2+ ✅
  • Firefox: Not yet supported ❌

Example:

if (await FlutterGemma.isStreamingSupported()) {
  await FlutterGemma.initialize(
    webStorageMode: WebStorageMode.streaming,
  );
} else {
  print('OPFS not available, using cacheApi mode');
  await FlutterGemma.initialize(
    webStorageMode: WebStorageMode.cacheApi,
  );
}

Implementation

static Future<bool> isStreamingSupported() async {
  // Streaming only supported on web platform
  if (!kIsWeb) {
    return false;
  }

  try {
    final registry = ServiceRegistry.instance;

    // Check if web storage mode is streaming
    if (registry.webStorageMode != WebStorageMode.streaming) {
      return false;
    }

    // Check if OPFS service is available
    final downloadService = registry.downloadService;
    if (downloadService is! WebDownloadService) {
      return false;
    }

    return downloadService.opfsService != null;
  } catch (e) {
    // ServiceRegistry not initialized or error checking
    return false;
  }
}