Files
proto_go_dart_demo/frontend/lib/services/grpc_client.dart
2026-02-23 13:02:22 +03:00

38 lines
1.1 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:grpc/grpc.dart';
import '../gen/notes/v1/notes.pbgrpc.dart';
/// Singleton gRPC клиент.
/// Подключается к Go бекенду на localhost:50051.
class GrpcClient {
static GrpcClient? _instance;
late final ClientChannel _channel;
late final NoteServiceClient noteService;
GrpcClient._() {
_channel = ClientChannel(
'localhost',
port: 50051,
options: const ChannelOptions(
credentials: ChannelCredentials.insecure(),
),
);
// noteService — типизированный клиент.
// Все методы (createNote, listNotes и т.д.)
// сгенерированы из proto и принимают/возвращают
// типизированные объекты. Невозможно передать
// неверный тип — ошибка компиляции Dart.
noteService = NoteServiceClient(_channel);
}
static GrpcClient get instance {
_instance ??= GrpcClient._();
return _instance!;
}
Future<void> shutdown() async {
await _channel.shutdown();
}
}