trpc_client 0.4.0 trpc_client: ^0.4.0 copied to clipboard
A minimal trpc client for Dart and Flutter.
trpc_client #
A simple trpc client for Dart Applications. It's minimal, just two dependencies (dart's own http library, and queues). Ideal for flutter apps that use tRPC as a backend.
Features #
- Run Query and Mutate operations against your tRPC server (expressjs, Next.js, Cloudflare Worker, ...)
- Generate bindings from your tRPC router, to have type safety on your queries and mutations. More info here.
Usage #
You just need to instantiate the TRPCClient
class with the trpc base endpoint. You can optionally pass headers:
// Import package
import "package:trpc_client/trpc_client.dart";
// Instantiate the client
final client = TRPCClient(
baseUri: "https://example.com/trpc",
headers: {"x-trpc-source": "Dart App"},
);
Now you can query, and mutate, with type safety:
final response = await client.query<Map<String, dynamic>>("route", payload: {"userId": "example"});
if (response.isError) {
print(response.errors);
} else {
final data = response.successResponse.data;
}
Usage with Riverpod (Provider) #
I recommend using it with riverpod, to make authentication easier:
@riverpod
TRPCClient trpcClient(TrpcClientRef ref) {
final sessionId = ref.watch(sessionIdProvider);
final client = TRPCClient(
baseUri: "https://example.com/trpc",
headers: {"Authorization": "Bearer $sessionId"},
);
return client;
}