ilteoood / flutter_i18n

I18n made easy, for Flutter!
MIT License
217 stars 57 forks source link

constatnlty reloading create issue #161

Closed Miamoto-Musashi closed 3 years ago

Miamoto-Musashi commented 3 years ago

the file are constantly reloading thus creating NPE isue

The following NoSuchMethodError was thrown building Consumer<OrganizationInfoModel>(dirty, dependencies: [_InheritedProviderScope<OrganizationInfoModel>, _LocalizationsScope-[GlobalKey#78eaf]]):
The method '[]' was called on null.
Receiver: null
Tried calling: []("")

The relevant error-causing widget was
ViewModelBuilder<OrganizationInfoModel>
lib/…/auth/organization_info_screen.dart:20
When the exception was thrown, this was the stack
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      SimpleTranslator._decodeFromMap
package:flutter_i18n/utils/simple_translator.dart:51
#2      SimpleTranslator._translateWithKeyFallback
package:flutter_i18n/utils/simple_translator.dart:42
#3      SimpleTranslator.translate
package:flutter_i18n/utils/simple_translator.dart:24
#4      FlutterI18n.translate
package:flutter_i18n/flutter_i18n.dart:101

configuration is done in main.dart

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    precacheImage(AssetImage('assets/images/dark_logo_transparent-no-write.png'), context);
    final flutterI18nDelegate = FlutterI18nDelegate(
      translationLoader: FileTranslationLoader(
        // useE2E: false,
        useCountryCode: false,
        fallbackFile: 'en',
        basePath: 'assets/i18n',
        forcedLocale: Locale('en'),
        decodeStrategies: [YamlDecodeStrategy()],
      ),
      missingTranslationHandler: (key, locale) {
        print('--- Missing Key: $key, languageCode: ${locale.languageCode}');
      },
    );

....

return MaterialApp(
      builder: ExtendedNavigator.builder<route.Router>(
        router: route.Router(),
        guards: [AuthGuard()],
      ),
      debugShowCheckedModeBanner: false,

      localizationsDelegates: [flutterI18nDelegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate],

when reload ends the screen is reloaded and red screen disappear

stack trace

Object.noSuchMethod (dart:core-patch/object_patch.dart:51)
SimpleTranslator._decodeFromMap (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/flutter_i18n-0.20.1/lib/utils/simple_translator.dart:51)
SimpleTranslator._translateWithKeyFallback (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/flutter_i18n-0.20.1/lib/utils/simple_translator.dart:42)
SimpleTranslator.translate (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/flutter_i18n-0.20.1/lib/utils/simple_translator.dart:24)
FlutterI18n.translate (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/flutter_i18n-0.20.1/lib/flutter_i18n.dart:101)
RequestListScreen.build.<anonymous closure> (/Users/enrico/progetti/truxy/customers/lib/ui/request/request_list_screen.dart:26)
_ViewModelBuilderState.builderWithDynamicSourceInitialise (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/stacked-1.7.7/lib/src/view_model_builder.dart:168)
Consumer.buildWithChild (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/provider-4.3.3/lib/src/consumer.dart:177)
SingleChildStatelessWidget.build (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/nested-0.0.4/lib/nested.dart:260)
StatelessElement.build (/Users/enrico/development/flutter/packages/flutter/lib/src/widgets/framework.dart:4701)
SingleChildStatelessElement.build (/Users/enrico/.pub-cache/hosted/pub.dartlang.org/nested-0.0.4/lib/nested.dart:280)
ComponentElement.performRebuild (/Users/enrico/development/flutter/packages/flutter/lib/src/widgets/framework.dart:4627)
Element.rebuild (/Users/enrico/development/flutter/packages/flutter/lib/src/widgets/framework.dart:4343)
BuildOwner.buildScope (/Users/enrico/development/flutter/packages/flutter/lib/src/widgets/framework.dart:2730)
WidgetsBinding.drawFrame (/Users/enrico/development/flutter/packages/flutter/lib/src/widgets/binding.dart:913)
RendererBinding._handlePersistentFrameCallback (/Users/enrico/development/flutter/packages/flutter/lib/src/rendering/binding.dart:302)
SchedulerBinding._invokeFrameCallback (/Users/enrico/development/flutter/packages/flutter/lib/src/scheduler/binding.dart:1117)
SchedulerBinding.handleDrawFrame (/Users/enrico/development/flutter/packages/flutter/lib/src/scheduler/binding.dart:1055)
SchedulerBinding._handleDrawFrame (/Users/enrico/development/flutter/packages/flutter/lib/src/scheduler/binding.dart:971)
_rootRun (dart:async/zone.dart:1190)
_CustomZone.run (dart:async/zone.dart:1093)
ilteoood commented 3 years ago

Hi Enrico, can you reproduce the issue with the example project? if not, can you provide me an example project in which this happens?

Miamoto-Musashi commented 3 years ago

Hi, I'm using the old version and cannot upgrade flutter to the new version.

here the YAML file I use maybe is related to num of rows

cheers

ilteoood commented 3 years ago

I tried to integrate your yml in the example app: it loads without problem.

Can you try to include WidgetsFlutterBinding.ensureInitialized(); under the delegate declaration?

Here is an example from the example app


Future main() async {
  final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
    translationLoader: FileTranslationLoader(
        useCountryCode: false,
        fallbackFile: 'en',
        basePath: 'assets/i18n',
        forcedLocale: Locale('es')),
  );
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp(flutterI18nDelegate));
}
Miamoto-Musashi commented 3 years ago

Hi, I have the ensure Initialized on the main class

void main() async {
  initializeReflectable();
  putLumberdashToWork(withClients: [ColorizeLumberdash(), SentryLumberdash.withDsn(dsnKey: Values.sentryDnsKey)]);

  // await setupSnackbarUi();
  WidgetsFlutterBinding.ensureInitialized();

  await setupHive();
  await setupLocator();
  await setupDialogUi();

  runApp(GetMaterialApp(home: MyApp()));
}

do you think it does change?

the problem BTW is not on first loading, the app loads well, the problem is that during a rebuild of the widget tree the .yml file is reloaded (I'm using filledstack MVVM architecture) and the rendering is done with a Null value; after the .yml reload is complete the screen reload again but now the value is correct and the rendering is successful

ilteoood commented 3 years ago

Can you tell me the version of the library that you're using?

Miamoto-Musashi commented 3 years ago

Hi, the version is flutter_i18n: ^0.21.1

ilteoood commented 3 years ago

On example app this issue can't be reproduced.

If you encounter this again, please reproduce it with the example app provided with this repository, or create a new one that can prove the bug.