Giter Site home page Giter Site logo

S.of(context) is null about gen_lang HOT 3 CLOSED

ankurda28 avatar ankurda28 commented on May 30, 2024
S.of(context) is null

from gen_lang.

Comments (3)

KingWu avatar KingWu commented on May 30, 2024

Did you setup MaterialApp?

MaterialApp(
      localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: S.delegate.supportedLocales,

from gen_lang.

KingWu avatar KingWu commented on May 30, 2024

Will closed it if no response

from gen_lang.

frojnd avatar frojnd commented on May 30, 2024

@KingWu Thank you for the pub.

I get similar if not the same error when calling S.of(context).card_0_0 for homepage title.

Here is the whole log:

I/flutter ( 7576): The following NoSuchMethodError was thrown building MyApp(dirty):
I/flutter ( 7576): The getter 'card_0_0' was called on null.
I/flutter ( 7576): Receiver: null
I/flutter ( 7576): Tried calling: card_0_0
I/flutter ( 7576): 
I/flutter ( 7576): The relevant error-causing widget was:
I/flutter ( 7576):   MyApp file:///home/joze/coding/flutterProjects/package_test/lib/main.dart:5:23
I/flutter ( 7576): 
I/flutter ( 7576): When the exception was thrown, this was the stack:
I/flutter ( 7576): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
I/flutter ( 7576): #1      MyApp.build (package:package_test/main.dart:31:45)
I/flutter ( 7576): #2      StatelessElement.build (package:flutter/src/widgets/framework.dart:4291:28)
I/flutter ( 7576): #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
I/flutter ( 7576): #4      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter ( 7576): #5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
I/flutter ( 7576): #6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter ( 7576): #7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter ( 7576): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter ( 7576): #9      RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1028:16)
I/flutter ( 7576): #10     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:999:5)
I/flutter ( 7576): #11     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:942:17)
I/flutter ( 7576): #12     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2412:19)
I/flutter ( 7576): #13     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:941:13)
I/flutter ( 7576): #14     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:819:7)
I/flutter ( 7576): #15     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:804:7)
I/flutter ( 7576): #24     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
I/flutter ( 7576): #25     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
I/flutter ( 7576): #26     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
I/flutter ( 7576): (elided 8 frames from package dart:async and package dart:async-patch)
I/flutter ( 7576): 
I/flutter ( 7576): ════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building MyApp(dirty):
The getter 'card_0_0' was called on null.
Receiver: null
Tried calling: card_0_0

The relevant error-causing widget was: 
  MyApp file:///home/joze/coding/flutterProjects/package_test/lib/main.dart:5:23
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      MyApp.build (package:package_test/main.dart:31:45)
#2      StatelessElement.build (package:flutter/src/widgets/framework.dart:4291:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

Example app

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:package_test/generated/i18n.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: S.delegate.supportedLocales,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),

      /*** IT FAILS BELOW THIS COMMENT ***/
      home: MyHomePage(title: S.of(context).card_0_0),

    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              S.of(context).card_0_0, // IT WORKS HERE
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

from gen_lang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.