localizely / flutter-intl-intellij

This Android Studio & IntelliJ plugin generates boilerplate code for localization of Flutter apps with official Dart Intl library
MIT License
129 stars 4 forks source link

The generated code does not come with null security. #82

Closed haoyuanwu closed 2 years ago

haoyuanwu commented 2 years ago

Each change will be revised, so please support the null security writing method as soon as possible

// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
// This is a library that looks up messages for specific locales by
// delegating to the appropriate library.

// Ignore issues from commonly used lints in this file.
// ignore_for_file:implementation_imports, file_names, unnecessary_new
// ignore_for_file:unnecessary_brace_in_string_interps, directives_ordering
// ignore_for_file:argument_type_not_assignable, invalid_assignment
// ignore_for_file:prefer_single_quotes, prefer_generic_function_type_aliases
// ignore_for_file:comment_references

import 'dart:async';

import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';
import 'package:intl/src/intl_helpers.dart';

import 'messages_en.dart' as messages_en;
import 'messages_zh.dart' as messages_zh;

typedef Future<dynamic> LibraryLoader();
Map<String, LibraryLoader> _deferredLibraries = {
  'en': () => new Future.value(null),
  'zh': () => new Future.value(null),
};

MessageLookupByLibrary? _findExact(String localeName) {
  switch (localeName) {
    case 'en':
      return messages_en.messages;
    case 'zh':
      return messages_zh.messages;
    default:
      return null;
  }
}

/// User programs should call this before using [localeName] for messages.
Future<bool> initializeMessages(String localeName) async {
  var availableLocale = Intl.verifiedLocale(
      localeName,
          (locale) => _deferredLibraries[locale] != null,
      onFailure: (_) => null);
  if (availableLocale == null) {
    return new Future.value(false);
  }
  var lib = _deferredLibraries[availableLocale];
  await (lib == null ? new Future.value(false) : lib());
  initializeInternalMessageLookup(() => new CompositeMessageLookup());
  messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
  return new Future.value(true);
}

bool _messagesExistFor(String locale) {
  try {
    return _findExact(locale) != null;
  } catch (e) {
    return false;
  }
}

MessageLookupByLibrary? _findGeneratedMessagesFor(String locale) {
  var actualLocale = Intl.verifiedLocale(locale, _messagesExistFor,
      onFailure: (_) => null);
  if (actualLocale == null) return null;
  return _findExact(actualLocale);
}

and

static S? current;

  static const AppLocalizationDelegate delegate =
  AppLocalizationDelegate();

  static Future<S> load(Locale locale) {
    final name = (locale.countryCode?.isEmpty ?? false) ? locale.languageCode : locale.toString();
    final localeName = Intl.canonicalizedLocale(name);
    return initializeMessages(localeName).then((_) {
      Intl.defaultLocale = localeName;
      S.current = S();

      return S.current!;
    });
  }

  static S? of(BuildContext context) {
    return Localizations.of<S>(context, S);
  }
lzoran commented 2 years ago

Hi @haoyuanwu,

Null-safety has been supported since Flutter Intl 1.13.0.

To enable the generation of null-safety code make sure that the sdk version is >=2.12.0 in the pubspec.yaml file and that you have the appropriate version of the Flutter Intl plugin that supports null-safety.

The pubspec.yaml file:

environment:
  sdk: ">=2.12.0 <3.0.0"
korilin commented 2 years ago

I have the same problem.

pubspec.ymal file of my project module:

environment:
  sdk: ">=2.12.0 <3.0.0"
  flutter: ">=2.10.0"

But generated code without null-safety when I connect plugin.

image

Are there any other conditions that need to be checked?

lzoran commented 2 years ago

Hi @korilin,

Which version of the Flutter Intl plugin are you using: 1.17.3-2019.2 or 1.17.3-2020.3? Maybe reinstalling the Flutter Intl plugin might help.

Also, the Flutter Intl plugin uses the intl_utils package under the hood to generate localization code. In case you do not have intl_utils ^2.0.0 activated globally (flutter pub global list), maybe reactivation might help.

  1. Deactivate the intl_utils package:

    flutter pub global deactivate intl_utils
  2. Trigger localization code generation to activate the proper version of the intl_utils (e.g. save update arb file).

korilin commented 2 years ago

Hi @lzoran,

Thanks for your help, I'm use Flutter Intl 1.17.3-2020.3.

After I executed flutter pub global deactivate intl_utils, the plugin was generate null-safety code normally.

lzoran commented 2 years ago

I'm closing this issue. In case you notice any irregularity, feel free to reopen it.