Flutter UI & Reactive BLoCs
The Flutter client layer exposes a unified, type-safe API barrel through package:<app>_flutter/api.dart. Hand-written Flutter screens in client/lib/ consume pre-configured Serverpod clients, authentication gates, and generated reactive BLoCs without boilerplate networking code.
The single import surface (api.dart)
Section titled “The single import surface (api.dart)”You never need to hunt through multiple generated folders. Every Flutter screen needs only one Aegil import:
import 'package:flutter/material.dart';import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:helpdesk_flutter/api.dart';What api.dart provides:
- All protocol models and enums (
Ticket,TicketStatus,TicketComment). - Pre-configured client:
createAppClient()initializes connectivity and session persistence. - Authentication gate:
SignInGateshows email sign-in whenever the user is logged out. - Generated BLoCs:
<Entity>Bloc, events, and states for every model with an@autoQuery.
App entry point (client/lib/main.dart)
Section titled “App entry point (client/lib/main.dart)”import 'package:flutter/material.dart';import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:helpdesk_flutter/api.dart';
late final Client client;
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); client = await createAppClient(); runApp(const HelpdeskApp());}
class HelpdeskApp extends StatelessWidget { const HelpdeskApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( title: 'Helpdesk', theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue)), home: SignInGate( client: client, child: BlocProvider( create: (_) => TicketBloc(client: client)..add(const ListTicketsRequested()), child: const TicketsScreen(), ), ), ); }}Using the generated <Entity>Bloc
Section titled “Using the generated <Entity>Bloc”For every model with an @autoQuery in your specs, Aegil generates a complete flutter_bloc implementation:
class TicketsScreen extends StatelessWidget { const TicketsScreen({super.key});
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Support Tickets')), body: BlocBuilder<TicketBloc, TicketState>( builder: (context, state) { if (state.status == TicketLoadStatus.loading && state.items.isEmpty) { return const Center(child: CircularProgressIndicator()); } if (state.status == TicketLoadStatus.failure && state.items.isEmpty) { return Center(child: Text('Failed to load tickets: ${state.error}')); } if (state.items.isEmpty) { return const Center(child: Text('No tickets found.')); } return ListView.builder( itemCount: state.items.length, itemBuilder: (context, index) { final ticket = state.items[index]; return ListTile( title: Text(ticket.title), subtitle: Text('Status: ${ticket.status.name}'), trailing: IconButton( icon: const Icon(Icons.arrow_upward), onPressed: () { context.read<TicketBloc>().add( EscalateTicketRequested( ticketId: ticket.id, assignedAgentId: 'agent-101', ), ); }, ), ); }, ); }, ), ); }}Generated BLoC capabilities
Section titled “Generated BLoC capabilities”- Query events:
List<Entity>Requested(...)dispatches queries with optional filtering parameters. - Mutation events:
Create<Entity>Requested(...)or custom mutation events execute backend operations and updatestate.items. - Real-time stream updates: If
@autoStreamis declared in specs, the BLoC subscribes to incoming WebSocket messages and updatesstate.itemswithout manual stream management. - State properties:
state.status:initial | loading | success | failure.state.items: Normalized list of entities.state.error: Error object (Object?) if an operation failed.
Configuring the Server URL
Section titled “Configuring the Server URL”createAppClient() resolves the Serverpod backend URL in this order:
--dart-define=SERVER_URL=https://api.example.com/passed at build/run time.apiUrlinclient/assets/config.json.http://localhost:8080/(default for local desktop/web).
Mobile physical devices: When running on a physical phone on your local Wi-Fi, pass
--dart-define=SERVER_URL=http://<your-computer-ip>:8080/to allow your phone to reach your local Serverpod server.
What could go wrong?
Section titled “What could go wrong?”| What you see | Why | What to do |
|---|---|---|
SocketException: Connection refused |
The Flutter client cannot reach the Serverpod server at localhost:8080. |
Ensure aegil run is running, or pass --dart-define=SERVER_URL for physical devices. |
Red screen: SignInGate unauthenticated |
An operation requires login, but the user is signed out. | Wrap your protected screens with SignInGate(client: client, child: ...). |
ERR_UI_BOUNDARY_VIOLATION |
Flutter code imported internal .aegil/client/lib/src/... files directly. |
Import strictly from package:<app>_flutter/api.dart. |
Next steps
Section titled “Next steps”- Learn Spec-Diff Database Migrations.
- Learn Running & Debugging Locally.
- Learn The 3-Step Agent Protocol.