Apple
Apple

Reputation: 151

Why does flutter localization not work properly?

I want to add the tr tag to the Flutter localization section. I am getting an error although there is support for Tr. The error is as follows; error: The element type 'Locale (where Locale is defined in C:\Users\StatTark\AppData\Roaming\Pub\Cache\hosted\pub.dartlang.org\intl-0.16.1\lib\src\locale.dart)' can't be assigned to the list type 'Locale (where Locale is defined in C:\flutter\bin\cache\pkg\sky_engine\lib\ui\window.dart)'.

error: Abstract classes can't be instantiated. (instantiate_abstract_class at [ajanda] lib\pages\mainmenu.dart:36)

I do not understand if I am making a mistake in use, I will be glad if you help.

class MainMenu extends StatelessWidget {
  MainMenu({Key key}) : super(key: key);
  final _sdb = SettingsDbHelper();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _sdb.getSettings(),
      builder: (context, snapshot) {
        if (snapshot.data == null) {
          return MaterialApp(
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate
            ],
            supportedLocales:  [Locale('en','US'),Locale('tr','')], //The error is here
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              body: Center(
                child: Text(proTranslate["Yükleniyor....."][Language.languageIndex]),
              ),
            ),
          );
        } else {
          Language.languageIndex = snapshot.data[0].language;
          return DynamicTheme(
              defaultBrightness: Brightness.light,
              data: (brightness) => ThemeData(
                    brightness: brightness,
                    fontFamily: snapshot.data[0].fontName,
                    floatingActionButtonTheme: FloatingActionButtonThemeData(
                      foregroundColor: Colors.green,
                    ),
                  ),
              themedWidgetBuilder: (context, theme) {
                return MaterialApp(
                  localizationsDelegates: [
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate
                  ],
                 supportedLocales:  [Locale('en','US'),Locale('tr','')],

                  debugShowCheckedModeBanner: false,
                  theme: theme,
                  home: MainMenuBody(
                    warning: snapshot.data[0].warning,
                  ),
                  // navigatorKey: navigatorKey,
                );
              });
        }
      },
    );
  }
}

class MainMenuBody extends StatefulWidget {....}

Upvotes: 0

Views: 7268

Answers (2)

Tim Tosi
Tim Tosi

Reputation: 305

I'm coming a bit late but I just fixed this same issue I had.

I'm pretty sure your file is importing intl/locale.dart instead of flutter/material.dart as both define a Local type.

To fix it, just replace your import at the top of the file from:

import 'package:intl/locale.dart';

to

import 'package:flutter/material.dart';

and you should be OK.

Upvotes: 1

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9755

Few suggestions you can try/test, see below.

  1. Replace [Locale('en','US'),Locale('tr','')], with [Locale('en'),Locale('tr')],

  2. init list of supported locales first and use it accordingly.

// init before build  
final List<Locale> appSupportedLocales = [
  Locale('en'),
  Locale('tr')
];

// ...
// then use it like this   
supportedLocales: appSupportedLocales,  

Upvotes: 0

Related Questions