Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The getter 'languageCode' was called on null flutter for iOS #39032

Closed
vprudnikoff opened this issue Aug 22, 2019 · 22 comments · Fixed by flutter/engine#17473
Closed

The getter 'languageCode' was called on null flutter for iOS #39032

vprudnikoff opened this issue Aug 22, 2019 · 22 comments · Fixed by flutter/engine#17473
Assignees
Labels
a: internationalization Supporting other languages or locales. (aka i18n) customer: crowd Affects or could affect many people, though not necessarily a specific customer. engine flutter/engine repository. See also e: labels. platform-ios iOS applications specifically
Milestone

Comments

@vprudnikoff
Copy link

vprudnikoff commented Aug 22, 2019

Hello!
Still have the error with localization for Flutter iOS application (for Android everything works fine).
My code for App widget:

...
@override
  Widget build(BuildContext context) {
    _initGlobalConstants();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    _bloc.dispatch(AppEventStarted());
    return MaterialApp(
      supportedLocales: [
        Locale('en', 'US'),
        Locale('ru', 'RU')
      ],
      localizationsDelegates: [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode) {
            return supportedLocale;
          }
        }
        return supportedLocales.first;
      } 
...

The point is that when I try to launch it with some breakpoints, everything works good and locale is not null. But when I try the same without any breakpoints, I have error inside localeResolutionCallback - The getter 'languageCode' was called on null. Tried several times some tips about this issue for iOS:

  1. Added
<key>CFBundleLocalizations</key>
	<array>
		<string>English</string>
		<string>ru</string>
	</array> 
...

for Info.plst
2) Added locales support for iOS project.

But nothing helped.
Don't know what to do. Localization is very important, I thing, but it's so ugly for Flutter now.

@HansMuller HansMuller added a: internationalization Supporting other languages or locales. (aka i18n) f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. platform-ios iOS applications specifically labels Aug 22, 2019
@nickrivers
Copy link

@prudnikoff
I have this problem now, come from nowhere, the app was working fine :(
Did you solve the problem?

@vprudnikoff
Copy link
Author

@nickrivers
I just removed localeResolutionCallback and everything works ok. But if you use app and changes system language at runtime, your app will be broken.

@nick-llewellyn
Copy link

nick-llewellyn commented Oct 16, 2019

I am getting the following on my test device an iPhone 7 iOS 13.1.2, running a production build based on the flutter stable branch :

NoSuchMethodError: The getter 'languageCode' was called on null.
Receiver: null
Tried calling languageCode

When I:

  1. Open the production build of my app on my test device.
  2. Open any other app moving my app to the background.
  3. Wait a minute or two.
  4. Bring my app back to the foreground by selecting it from the running app picker or tapping it's icon.

This only happens on physical iOS devices running a production build. Can’t test on a physical iOS device in debug mode on stable at the moment.

If I shutdown my app app and reopen it the error still occurs, I have to uninstall the app or power off the device to get my app working again.

This wasn't happening when the app was built using older versions of flutter stable, unfortunately I didn't notice at which version of flutter stable it started happening.

flutter doctor -v
[✓] Flutter (Channel stable, v1.9.1+hotfix.4, on Mac OS X 10.14.6 18G103, locale en-GB)
• Flutter version 1.9.1+hotfix.4 at /Users/nicholasllewellyn/Development/Tools/flutter
• Framework revision cc949a8 (3 weeks ago), 2019-09-27 15:04:59 -0700
• Engine revision b863200
• Dart version 2.5.0

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/nicholasllewellyn/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /Users/nicholasllewellyn/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/191.5900203/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.1, Build version 11A1027
• CocoaPods version 1.8.3

[✓] Android Studio (version 3.5)
• Android Studio at /Users/nicholasllewellyn/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/191.5900203/Android
Studio.app/Contents
• Flutter plugin version 40.2.2
• Dart plugin version 191.8423
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.39.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.5.1

[!] Connected device
! No devices available

! Doctor found issues in 1 category.

@mehmetyilmaz001
Copy link

Same problem here. When I deploy the app to the pychical device it is hapennig.

@alange-pivotal
Copy link

Same for me also running in debug mode on iOS Emulator, ios 13.3.

@kinex
Copy link

kinex commented Dec 20, 2019

Maybe a related problem is that window.locale is null on app startup sometimes. I have this on app startup (before runApp):

WidgetsFlutterBinding.ensureInitialized();
final locale = window.locale;
Intl.systemLocale = locale.toString();

Works fine on Android, causes random crash on iOS (device).

@kinex
Copy link

kinex commented Dec 20, 2019

Workaround to my issue:

WidgetsFlutterBinding.ensureInitialized();
while (window.locale == null) {
   await Future.delayed(const Duration(milliseconds: 1));
}
final locale = window.locale;
Intl.systemLocale = locale.toString();

Not pretty but it works...

@devapalanisamy
Copy link

Issue still persists in 2020

@kelvinmontini
Copy link

kelvinmontini commented Mar 9, 2020

Hello,
Any workaround to resolve?

Currently I removed the localeResolutionCallback, but in future i'll have some problems with it.


localeResolutionCallback: (locale, supportedLocales) {
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode &&
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }
        return supportedLocales.first;
      },

@VladyslavBondarenko VladyslavBondarenko added the customer: crowd Affects or could affect many people, though not necessarily a specific customer. label Mar 13, 2020
@dnfield
Copy link
Contributor

dnfield commented Apr 1, 2020

@GaryQian do you know what's going on here? I remember you doing some work on locales on iOS, I think.

@GaryQian
Copy link
Contributor

GaryQian commented Apr 1, 2020

I will look into it!

@dnfield
Copy link
Contributor

dnfield commented Apr 1, 2020

One thing that looks fishy to me - in FlutterViewController.mm, we're not calling the flow to update the locales until viewDidAppear. It seems like we may need to call that earlier.

@patniemeyer
Copy link

This issue means that localized Flutter apps may be initially rendered with the wrong locale and (usually but not always) re-drawn later with the correct one. This is tremendously problematic for things like dialogs and screens pushed on launch that can end up "capturing" the wrong locale. I can't believe that this problem isn't more widely noticed.

@dnfield dnfield self-assigned this Apr 2, 2020
@dnfield dnfield added this to the April 2020 milestone Apr 2, 2020
@dnfield
Copy link
Contributor

dnfield commented Apr 2, 2020

I have a fix for this. Working on writing a test for it.

@dnfield
Copy link
Contributor

dnfield commented Apr 2, 2020

For people looking for a workaround, copying what I wrote on a similar bug:


The patch for this may take some time before it rolls on to a usable channel for people experiencing this bug. For now, this can be worked around by using a platform channel when on iOS to request the current locale from the device, as explained here: https://flutter.dev/docs/development/platform-integration/platform-channels#step-4-add-an-ios-platform-specific-implementation

Alternatively, you could draw a splash screen or animation until the locale initializes (and you can check that properly by adding an observer to the WidgetsBinding that overrides didChangeLocales), e.g. something like

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (WidgetsBinding.instance.window.locale == null) {
    Completer<Locale> completer = Completer<Locale>();
    WidgetsBinding.instance.addObserver(LocaleObserver(completer));
    // completer.future is now a `Future` that will complete when the locale is available.
  }
}

class LocaleObserver extends WidgetsBindingObserver {
  LocaleObserver(this.completer);

  final Completer<Locale> completer;

  @override
  void didChangeLocale(List<Locale> locale) {
    WidgetsBinding.instance.removeObserver(this);
    completer.complete(WidgetsBinding.instance.window.locale);
  }
}

@dnfield dnfield added engine flutter/engine repository. See also e: labels. and removed f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Apr 2, 2020
@dnfield
Copy link
Contributor

dnfield commented Apr 2, 2020

Even better work around:

Add this to your AppDelegate.swift:

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    // Work-around for https://github.com/flutter/flutter/issues/39032
    // This is safe to remove once the fix for that is available in the SDK
    // used by this application.
    guard let controller = window?.rootViewController as? FlutterViewController else {
      fatalError("rootViewController is not type FlutterViewController")
    }
    let engine = controller.engine!
    let currentLocale = NSLocale.current
    if (currentLocale.languageCode != nil) {
      engine.localizationChannel?.invokeMethod("setLocale", arguments: [currentLocale.languageCode!, currentLocale.regionCode ?? "", currentLocale.scriptCode ?? "", currentLocale.variantCode ?? ""])
    }

patniemeyer added a commit to OrchidTechnologies/orchid that referenced this issue Apr 3, 2020
@raugaral
Copy link

Will this correction be released soon to any branch?

@dnfield
Copy link
Contributor

dnfield commented Apr 16, 2020

It's in 1.17.5 dev and in all the 1.18.x series.

@d9media
Copy link

d9media commented Jun 8, 2020

Switching channels to dev seemed to have fixed issue for us.

@ozgurmacit
Copy link

This issue is still not solved for 1.20.

@mrigankdoshy
Copy link

Any fix for this yet?

@github-actions
Copy link

github-actions bot commented Aug 9, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: internationalization Supporting other languages or locales. (aka i18n) customer: crowd Affects or could affect many people, though not necessarily a specific customer. engine flutter/engine repository. See also e: labels. platform-ios iOS applications specifically
Projects
None yet
Development

Successfully merging a pull request may close this issue.