Flutter startup network blocking

From divinevideo/divine-mobile · View interactive version

Flutter startup network blocking is an AI agent skill from the GitHub repository divinevideo/divine-mobile. Fix slow Flutter app startup caused by blocking network operations during initialization. Use when: (1) App takes several seconds to show first frame, (2) Startup logs show sequential network operations (WebSocket connections, API calls), (3) Services initialize during startup that aren't needed unt

How to use this skill

Skill file

Flutter Startup Network Blocking

Problem

Flutter app startup is slow because network operations (WebSocket connections, API calls,

service initialization) run sequentially during the initialization phase, blocking the

first frame from rendering. With multiple relay/server connections, worst-case startup

time becomes O(n × timeout) instead of O(max timeout).

Context / Trigger Conditions

Solution

1. Identify Blocking Operations

Look for sequential awaits in startup code:

// BAD: Sequential - each connection blocks the next
for (final url in relays) {
  await connectToRelay(url);  // Blocks startup!
}

2. Convert Sequential to Parallel

Use Future.wait() to run all connections simultaneously:

// GOOD: Parallel - all connections run at once
final results = await Future.wait(
  relays.map((url) async {
    final success = await connectToRelay(url);
    return MapEntry(url, success);
  }),
);

3. Defer Non-Critical Service Initialization

Move network-dependent services out of the critical startup path:

Before (blocking startup):

Future<void> _initializeCoreServices(ProviderContainer container) async {
  await container.read(authServiceProvider).initialize();
  await container.read(nostrServiceProvider).initialize();  // BLOCKS for relay connections!
  await container.read(otherServiceProvider).initialize();
}

After (lazy initialization):

Future<void> _initializeCoreServices(ProviderContainer container) async {
  // NOTE: NostrService initializes lazily when user authenticates
  await container.read(authServiceProvider).initialize();
  await container.read(seenVideosServiceProvider).initialize();
  // NostrService NOT initialized here - happens when auth state changes
}

4. Use Provider Dependencies for Lazy Init

Let Riverpod handle lazy initialization through provider dependencies:

@riverpod
NostrClient nostrClient(NostrClientRef ref) {
  final authService = ref.watch(authServiceProvider);

  // Only creates client when auth state is ready
  if (!authService.isAuthenticated) {
    return NostrClient.disconnected();
  }

  // Initialize lazily when actually needed
  final client = NostrClient(relays: authService.userRelays);
  Future.microtask(() => client.initialize());
  return client;
}

Verification

  1. Check startup logs for "First frame rendered in Xms" - should be < 2000ms
  2. Verify network operations happen AFTER first frame timestamp in logs
  3. For unauthenticated users, relay connections should NOT appear in startup logs

Example

Startup improvement achieved:

Log pattern showing fix working:

[18:15:17.538] First frame rendered in 1426ms
[18:15:17.556] Creating NostrClient...  // AFTER first frame!

Notes

Related Patterns

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.