vscode-flutter-i18n-json icon indicating copy to clipboard operation
vscode-flutter-i18n-json copied to clipboard

type 'DefaultWidgetsLocalizations' is not a subtype of type 'I18n'

Open Tridie2000 opened this issue 5 years ago • 4 comments

Sometimes I get this error. The only way to work around the issue is by passing the context of the main screen of the app. This results in a very complex codebase. If I change I18n.of(context).text to MaterialLocalizations.of(context).cancelButtonLabel, it works just fine. What could be the reason for this and is there an easy way to fix it?

Tridie2000 avatar Jul 22 '19 14:07 Tridie2000

download demo project

I made a demo project showing you how to reproduce this error. It includes this code:

class FrameScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text('Localization'),
            bottom: TabBar(
              tabs: <Widget>[
                Tab(
                  child: Text('1'),
                ),
                Tab(
                  child: Text('2'),
                ),
              ],
            ),
          ),
          body: TabBarView(
            children: <Widget>[
              MyHomePage(
                title: 'Test',
              ),
              Container(),
            ],
          ),
        ),
      ),
    );
  }
}

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(I18n.of(context).pushed),
            //Text(MaterialLocalizations.of(context).cancelButtonLabel),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Tridie2000 avatar Jul 22 '19 15:07 Tridie2000

Hi, back from vacation. Will have a look.

esskar avatar Jul 24 '19 06:07 esskar

Yes, I have the same error, did you find a solution?

abumalick avatar Mar 26 '20 18:03 abumalick

I think it happens when we forget to add supportedLocales, after adding it everything worked for me

abumalick avatar Mar 26 '20 18:03 abumalick