Flutter Localization Operations

31.07.2020 22:54
#FLUTTER #HATA

Flutter Localization Operations

When I used a ShowDatePicker in Flutter, the language was in English as the default, and the error I received when I wanted to make the language in Turkish using the locale feature was as follows.

The method 'formatMediumDate' was called on null.
Receiver: null
Tried calling: formatMediumDate(Instance of 'DateTime')

The problem here occurred as follows.

locale: const Locale('tr','TR'),

I made a definition like this and when I activated DatePicker, the above error occurred.

The error here is an error caused by incomplete use. To solve this error, we need to know how to do Localizations in Flutter.

Localization Operations in Flutter

1.First, under dependencies in the pubspec.yaml file,

flutter_localizations:
sdk: flutter

The above code will be written and packages.get operation will be done.

2. We need to define the languages ​​that we will use in our project in Main.dart. For this, under MaterialApp,

localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'),
const Locale('ru', 'RU'),
const Locale('tr', 'TR'),
],

The above code will be written. And the languages ​​that can be used in the system will be recorded. Then, new languages ​​can be defined one after the other if desired.

3. After these two definitions have been made, we can activate what we want from the defined languages ​​by making a definition such as const Locale ('tr', 'TR') to the locale property under the widget we wish.