Skip to content

Instantly share code, notes, and snippets.

@rifkyputra
Created April 21, 2022 11:21
Show Gist options
  • Select an option

  • Save rifkyputra/56e6ee9b1956001a0521dc86d887adf7 to your computer and use it in GitHub Desktop.

Select an option

Save rifkyputra/56e6ee9b1956001a0521dc86d887adf7 to your computer and use it in GitHub Desktop.

Revisions

  1. rifkyputra created this gist Apr 21, 2022.
    76 changes: 76 additions & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    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,
    );
    }
    }