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 '{string_key}' was called on null. #35

Closed
ammar-esrawi opened this issue Jan 9, 2019 · 14 comments
Closed

The getter '{string_key}' was called on null. #35

ammar-esrawi opened this issue Jan 9, 2019 · 14 comments

Comments

@ammar-esrawi
Copy link

static S of(BuildContext context) => Localizations.of<S>(context, S); ,
this call returns null
so the usage like will produce the exception The getter '{string_key}' was called on null.

@long1eu
Copy link
Owner

long1eu commented Jan 10, 2019

I need more info.

@ammar-esrawi
Copy link
Author

@long1eu
I think the problem comes from the flutter frame work it self
but in general the issue occur once you try to reach the S.of(context).SOMETHING inside a StateFull widget this will make the call return Localizations.of<S>(context, S); returns null instead of instance from S class

,i created another issue in the flutter repo it contains more details
please follow this

i am using this SDK

environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"

@long1eu
Copy link
Owner

long1eu commented Jan 10, 2019 via email

@long1eu
Copy link
Owner

long1eu commented Jan 10, 2019

Feel free to reopen the issues if you have the same problem after following the instructions.

@long1eu long1eu closed this as completed Jan 10, 2019
@ammar-esrawi
Copy link
Author

i tested your sample app and it did not work for me
in your app you setup the delegates and supported locals but did not call the values, i cloned your app and only changed one line in the main.dart file

home: MyHomePage(title: S.of(context).app_name),


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      onGenerateTitle: (BuildContext context) => S.of(context).app_name,
      localizationsDelegates: const <LocalizationsDelegate<WidgetsLocalizations>>[
        S.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
      localeResolutionCallback:
          S.delegate.resolution(fallback: const Locale('en', '')),
      localeListResolutionCallback:
          S.delegate.listResolution(fallback: const Locale('en', '')),
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: S.of(context).app_name),
    );
  }
}

this generated the following log

Initializing hot reload...
Syncing files to device Android SDK built for x86...
I/flutter ( 3688): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3688): The following NoSuchMethodError was thrown building MyApp(dirty):
I/flutter ( 3688): The getter 'app_name' was called on null.
I/flutter ( 3688): Receiver: null
I/flutter ( 3688): Tried calling: app_name
I/flutter ( 3688): 
I/flutter ( 3688): When the exception was thrown, this was the stack:
I/flutter ( 3688): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
I/flutter ( 3688): #1      MyApp.build (package:test_app/main.dart:23:45)
I/flutter ( 3688): #2      StatelessElement.build (package:flutter/src/widgets/framework.dart:3774:28)
I/flutter ( 3688): #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3721:15)
I/flutter ( 3688): #4      Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 3688): #5      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2286:33)
I/flutter ( 3688): #6      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:676:20)
I/flutter ( 3688): #7      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
I/flutter ( 3688): #8      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 3688): #9      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 3688): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:751:7)
I/flutter ( 3688): #12     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
I/flutter ( 3688): #13     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
I/flutter ( 3688): #14     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 3688): (elided one frame from package dart:async)
I/flutter ( 3688): ════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 1 of 420 libraries in 1,341ms.
 

note :
i can not reopen the issue

Thank you for the very useful plugin

@ammar-esrawi
Copy link
Author

dart versaion
image

flutter version
image

@long1eu long1eu reopened this Jan 14, 2019
@long1eu long1eu closed this as completed Jan 14, 2019
@long1eu
Copy link
Owner

long1eu commented Jan 14, 2019

Your you can't use S with that context. S.of(context) will always return null.

@long1eu
Copy link
Owner

long1eu commented Jan 14, 2019

try it like this

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      onGenerateTitle: (BuildContext context) => S.of(context).app_name,
      localizationsDelegates: const <LocalizationsDelegate<WidgetsLocalizations>>[
        S.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
      localeResolutionCallback: S.delegate.resolution(fallback: const Locale('en', '')),
      localeListResolutionCallback: S.delegate.listResolution(fallback: const Locale('en', '')),
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Builder(
        builder: (BuildContext context) {
          return MyHomePage(title: S.of(context).app_name);
        },
      ),
    );
  }
}

@ammar-esrawi
Copy link
Author

try it like this

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      onGenerateTitle: (BuildContext context) => S.of(context).app_name,
      localizationsDelegates: const <LocalizationsDelegate<WidgetsLocalizations>>[
        S.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
      localeResolutionCallback: S.delegate.resolution(fallback: const Locale('en', '')),
      localeListResolutionCallback: S.delegate.listResolution(fallback: const Locale('en', '')),
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Builder(
        builder: (BuildContext context) {
          return MyHomePage(title: S.of(context).app_name);
        },
      ),
    );
  }
}

this worked fine thank you

@long1eu
Copy link
Owner

long1eu commented Jan 14, 2019 via email

@ammar-esrawi
Copy link
Author

ammar-esrawi commented Jan 14, 2019

Yep i have been using the widget context since your generated Class S deppend on Localizations which only work in Material context so it worked through the builder

I think these information need to be documented in Flutter website cuz i read the documents but this important info were not presented

I appreciate your efforts

Also i wish if you can work on a plugin for the toJson ,fromJson also, they are annoying issue

@long1eu
Copy link
Owner

long1eu commented Jan 14, 2019

try https://pub.dartlang.org/packages/built_value

@po1arbear
Copy link

delete the 'generated' package, then it will recreate automatic ,solved my problem

@ahmedGibran
Copy link

ahmedGibran commented Feb 16, 2021

Isolate the user interface from the main application and it will work for you.
‏لقطة الشاشة 2021-02-17 في 12 18 41 ص

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants