onestudio-co / bond-core

Flutter package which contain the core file of one studio apps.
10 stars 6 forks source link

ValidatorLocalizations is not registered inside GetIt. #119

Open maykhid opened 2 months ago

maykhid commented 2 months ago

I started experimenting with the bond_form_riverpod today. After setting up I was getting this error: Unhandled Exception: Bad state: GetIt: Object/factory with type ValidatorLocalizations is not registered inside GetIt. I use get_it for my development. When I saw that error I immediately knew it wasn't an issue explicitly from my code because I didn't have any dependencies such as "ValidatorLocalizations", it started immediately after I set up bond_form_riverpod.

I did some digging into your code sample and I found out that ValidatorLocalizations.load(const Locale('en')) was registered somewhere as a dependency, I did the same thing and it started working for me.

So my questions are:

salahamassi commented 2 months ago

Hi there!

Thanks for bringing this up, and I'm glad to hear you're experimenting with bond_form_riverpod! Let me address your questions one by one.

1. Is that always necessary when using Bond Form?

Yes, registering ValidatorLocalizations is necessary when using bond_form because it relies on localized validation messages to provide user-friendly feedback in different languages. ValidatorLocalizations is the class that handles these localized messages, so it needs to be available as a dependency.

2. Why is that necessary?

The bond_form package uses ValidatorLocalizations to fetch localized strings for validation error messages. For instance, if a form field is required, the package will use ValidatorLocalizations to retrieve the correct error message in the user's language. Without registering this dependency, the package wouldn't be able to provide these messages, leading to the Bad state: GetIt: Object/factory with type ValidatorLocalizations is not registered inside GetIt error you encountered.

3. Did I miss something? Why is it that way?

You didn't necessarily miss anything, but it’s understandable why this could be confusing if it's not explicitly mentioned in the setup documentation.

When using bond_form or bond_form_riverpod, it’s essential to ensure that ValidatorLocalizations is registered in your dependency injection system (in this case, get_it). The package assumes that this registration is in place because validation error messages are a core part of form handling.

How to Set It Up

Here’s how you can properly set it up without relying on the bond_core package:

import 'dart:ui';
import 'package:get_it/get_it.dart';
import 'package:bond_form/validator_localizations.dart';

// Initialize GetIt
final getIt = GetIt.instance;

// Ensure that ValidatorLocalizations is registered
void setupDependencies() {
  getIt.registerSingletonAsync<ValidatorLocalizations>(
    () async => await ValidatorLocalizations.load(const Locale('en')),
  );
}

// Call this during your app initialization
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  setupDependencies();

  // Wait for all async dependencies to be ready
  await getIt.allReady();

  runApp(MyApp());
}

This code snippet registers ValidatorLocalizations with get_it manually, ensuring that bond_form can retrieve it when needed.

Final Thoughts

Including this step as part of your application's initialization process is crucial, particularly if you're using bond_form or any related packages. If you find this setup process cumbersome or error-prone, I highly recommend exploring the Flutter Bond template. The template is designed to simplify and streamline the configuration process, including all necessary dependencies and best practices right out of the box. This way, you can focus more on building your application rather than managing dependencies.

By using the Flutter Bond template, you'll also benefit from a well-structured project setup, along with additional tools and features provided by the Bond ecosystem. It's a great way to get up and running quickly and ensure that all necessary configurations are handled seamlessly.

Let me know if this helps, or if you have any further questions!

maykhid commented 2 months ago

I see. Indeed the fact that it was not mentioned anywhere in the doc was what made me confused.

I use get_it and I understand how it works. But for someone who doesn't or someone who doesn't want to use it, does that mean they have to explicitly use it, or is there another way?