1

I am new to flutter.

In my application locale information is found when the user login. So the idea is when the user login, it will pass the locale to AppLanguage.

I have written ChangeNotifierProxyProvider to get the locale inside authentication information and create a AppLanuage object

In the ChangeNotifierProxyProvider I am getting appLang as null. auth object is correctly NOT null.

What I don't understand why I am getting null?

I did create it here right?

create: (_) => AppLanguage(),

shouldn't it come as a parameter for the update?

Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: Auth()),
        ChangeNotifierProxyProvider<Auth, AppLanguage>(
            create: (_) => AppLanguage(),
            update: (ctx, auth, appLang) {
              print(auth);
              print(appLang);
            }
            //appLang.setLocale(auth == null ? 'en' : auth.language),
            ),
      ],
      child: Consumer2<Auth, AppLanguage>(
        builder: (ctx, auth, lang, child) => MaterialApp(
          title: 'Test App',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          locale: lang.appLocal,
          supportedLocales: [
            const Locale('en', 'US'),
            const Locale('ja', ''),
          ],
          localizationsDelegates: [
            AppLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          home: LandingView(),
        ),
      ),
    );
  }

1 Answer 1

0

I would try something like:

ChangeNotifierProxyProvider<Auth, AppLanguage>(
  create: (_) => AppLanguage(),
  update: (ctx, auth, appLang) => appLang..update(auth),
),
class AppLanguage with ChangeNotifier {
  void update(Auth auth) {
    // Do some custom work based on myModel that may call `notifyListeners`
  }
}

That way your child will be able to get the correct updated values when they're available.

You can check more how to deal properly with that on the provider docs.

2
  • what does double dots in "appLang..update(auth)" do?
    – Janaka
    Sep 24, 2020 at 15:57
  • returns the appLang model with new values accordingly to the new auth value Sep 24, 2020 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.