Skip to content

Instantly share code, notes, and snippets.

@rifkyputra
Created April 21, 2022 11:21
Show Gist options
  • Save rifkyputra/56e6ee9b1956001a0521dc86d887adf7 to your computer and use it in GitHub Desktop.
Save rifkyputra/56e6ee9b1956001a0521dc86d887adf7 to your computer and use it in GitHub Desktop.
client main - GRPC Chat App with Flutter and Dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final settingsController = SettingsController(SettingsService());
await settingsController.loadSettings();
final channel = ClientChannel(
EnvConfig.grpcAddress,
port: EnvConfig.grpcPort,
options: const ChannelOptions(
credentials: ChannelCredentials.insecure(),
),
);
final ChatMessagingServiceClient grpcClient =
ChatMessagingServiceClient(channel);
final storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
: await getTemporaryDirectory());
HydratedBlocOverrides.runZoned(
() => runApp(
MyAppProviderWrapper(
grpcClient: grpcClient,
child: MyApp(
settingsController: settingsController,
),
),
),
storage: storage,
);
}
class EnvConfig {
static const int grpcPort = int.fromEnvironment(
'GRPC_PORT',
defaultValue: 54242,
);
static const String grpcAddress = String.fromEnvironment(
'GRPC_ADDR',
defaultValue: 'localhost',
);
}
class MyAppProviderWrapper extends StatelessWidget {
const MyAppProviderWrapper({
Key? key,
required this.child,
required this.grpcClient,
}) : super(key: key);
final Widget child;
final ChatMessagingServiceClient grpcClient;
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => MessageIncomingCubit(grpcClient: grpcClient),
),
BlocProvider(
create: (context) => MessageSendCubit(grpcClient: grpcClient),
),
BlocProvider(
create: (context) => AppCubit(),
),
],
child: child,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment