Skip to content

Commit 42b4c11

Browse files
committed
migrate to code analyze flutter_lints
1 parent bd3fcbc commit 42b4c11

12 files changed

+243
-238
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#https://dart.dev/guides/language/analysis-options
22
#include: package:pedantic/analysis_options.yaml
3+
include: package:flutter_lints/flutter.yaml

bin/generate.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ String _resolve(Map<String, dynamic> translations, bool? skipUnnecessaryKeys,
219219
if (!_preservedKeywords.contains(key)) {
220220
accKey != null && !ignoreKey
221221
? fileContent +=
222-
' static const ${accKey.replaceAll('.', '_')}\_$key = \'$accKey.$key\';\n'
222+
' static const ${accKey.replaceAll('.', '_')}_$key = \'$accKey.$key\';\n'
223223
: !ignoreKey
224224
? fileContent += ' static const $key = \'$key\';\n'
225225
: null;
@@ -260,12 +260,12 @@ class CodegenLoader extends AssetLoader{
260260

261261
Map<String, dynamic>? data = json.decode(await fileData.readAsString());
262262

263-
final mapString = JsonEncoder.withIndent(' ').convert(data);
263+
final mapString = const JsonEncoder.withIndent(' ').convert(data);
264264
gFile += 'static const Map<String,dynamic> $localeName = $mapString;\n';
265265
}
266266

267267
gFile +=
268-
'static const Map<String, Map<String,dynamic>> mapLocales = \{${listLocales.join(', ')}\};';
268+
'static const Map<String, Map<String,dynamic>> mapLocales = {${listLocales.join(', ')}};';
269269
classBuilder.writeln(gFile);
270270
}
271271

lib/src/easy_localization_app.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ class EasyLocalization extends StatefulWidget {
9494
}
9595

9696
@override
97+
// ignore: library_private_types_in_public_api
9798
_EasyLocalizationState createState() => _EasyLocalizationState();
9899

100+
// ignore: library_private_types_in_public_api
99101
static _EasyLocalizationProvider? of(BuildContext context) =>
100102
_EasyLocalizationProvider.of(context);
101103

@@ -207,11 +209,11 @@ class _EasyLocalizationProvider extends InheritedWidget {
207209
// Locale get startLocale => parent.startLocale;
208210

209211
/// Change app locale
210-
Future<void> setLocale(Locale _locale) async {
212+
Future<void> setLocale(Locale locale) async {
211213
// Check old locale
212-
if (_locale != _localeState.locale) {
213-
assert(parent.supportedLocales.contains(_locale));
214-
await _localeState.setLocale(_locale);
214+
if (locale != _localeState.locale) {
215+
assert(parent.supportedLocales.contains(locale));
216+
await _localeState.setLocale(locale);
215217
}
216218
}
217219

lib/src/easy_localization_controller.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class EasyLocalizationController extends ChangeNotifier {
8888
if (useFallbackTranslations && _fallbackLocale != null) {
8989
Map<String, dynamic>? baseLangData;
9090
if (_locale.countryCode != null && _locale.countryCode!.isNotEmpty) {
91-
baseLangData = await loadBaseLangTranslationData(Locale(locale.languageCode));
91+
baseLangData =
92+
await loadBaseLangTranslationData(Locale(locale.languageCode));
9293
}
9394
data = await loadTranslationData(_fallbackLocale!);
9495
if (baseLangData != null) {
@@ -103,7 +104,8 @@ class EasyLocalizationController extends ChangeNotifier {
103104
}
104105
}
105106

106-
Future<Map<String, dynamic>?> loadBaseLangTranslationData(Locale locale) async {
107+
Future<Map<String, dynamic>?> loadBaseLangTranslationData(
108+
Locale locale) async {
107109
try {
108110
return await loadTranslationData(Locale(locale.languageCode));
109111
} on FlutterError catch (e) {
@@ -133,24 +135,24 @@ class EasyLocalizationController extends ChangeNotifier {
133135

134136
Future<void> _saveLocale(Locale? locale) async {
135137
if (!saveLocale) return;
136-
final _preferences = await SharedPreferences.getInstance();
137-
await _preferences.setString('locale', locale.toString());
138+
final preferences = await SharedPreferences.getInstance();
139+
await preferences.setString('locale', locale.toString());
138140
EasyLocalization.logger('Locale $locale saved');
139141
}
140142

141143
static Future<void> initEasyLocation() async {
142-
final _preferences = await SharedPreferences.getInstance();
143-
final _strLocale = _preferences.getString('locale');
144-
_savedLocale = _strLocale != null ? _strLocale.toLocale() : null;
145-
final _foundPlatformLocale = await findSystemLocale();
146-
_deviceLocale = _foundPlatformLocale.toLocale();
144+
final preferences = await SharedPreferences.getInstance();
145+
final strLocale = preferences.getString('locale');
146+
_savedLocale = strLocale?.toLocale();
147+
final foundPlatformLocale = await findSystemLocale();
148+
_deviceLocale = foundPlatformLocale.toLocale();
147149
EasyLocalization.logger.debug('Localization initialized');
148150
}
149151

150152
Future<void> deleteSaveLocale() async {
151153
_savedLocale = null;
152-
final _preferences = await SharedPreferences.getInstance();
153-
await _preferences.remove('locale');
154+
final preferences = await SharedPreferences.getInstance();
155+
await preferences.remove('locale');
154156
EasyLocalization.logger('Saved locale deleted');
155157
}
156158

lib/src/localization.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ class Localization {
9595

9696
String _replaceArgs(String res, List<String>? args) {
9797
if (args == null || args.isEmpty) return res;
98-
args.forEach((String str) => res = res.replaceFirst(_replaceArgRegex, str));
98+
for (var str in args) {
99+
res = res.replaceFirst(_replaceArgRegex, str);
100+
}
99101
return res;
100102
}
101103

@@ -119,8 +121,8 @@ class Localization {
119121
String? name,
120122
NumberFormat? format,
121123
}) {
122-
late var pluralCase;
123-
late var res;
124+
late PluralCase pluralCase;
125+
late String res;
124126
var pluralRule = _pluralRule(_locale.languageCode, value);
125127
switch (value) {
126128
case 0:
@@ -169,7 +171,7 @@ class Localization {
169171
}
170172

171173
String _gender(String key, {required String gender}) {
172-
return _resolve(key + '.$gender');
174+
return _resolve('$key.$gender');
173175
}
174176

175177
String _resolvePlural(String key, String subKey) {

lib/src/public.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ String tr(
3737
Map<String, String>? namedArgs,
3838
String? gender,
3939
}) {
40-
return Localization.instance.tr(key, args: args, namedArgs: namedArgs, gender: gender);
40+
return Localization.instance
41+
.tr(key, args: args, namedArgs: namedArgs, gender: gender);
4142
}
4243

4344
/// {@template plural}

lib/src/widgets.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@ import 'package:flutter/material.dart';
22

33
class FutureErrorWidget extends StatelessWidget {
44
final String msg;
5-
const FutureErrorWidget({this.msg = 'Loading ...'});
5+
const FutureErrorWidget({Key? key, this.msg = 'Loading ...'})
6+
: super(key: key);
67
@override
78
Widget build(BuildContext context) {
89
return Container(
910
color: Colors.white,
1011
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
11-
Icon(
12+
const Icon(
1213
Icons.error_outline,
1314
color: Colors.red,
1415
size: 64,
1516
textDirection: TextDirection.ltr,
1617
),
17-
SizedBox(height: 20),
18-
Text(
18+
const SizedBox(height: 20),
19+
const Text(
1920
'Easy Localization:',
2021
textAlign: TextAlign.center,
2122
textDirection: TextDirection.ltr,
2223
style: TextStyle(
2324
fontWeight: FontWeight.w700, color: Colors.red, fontSize: 25.0),
2425
),
25-
SizedBox(height: 10),
26+
const SizedBox(height: 10),
2627
Text(
2728
'"$msg"',
2829
textAlign: TextAlign.center,
2930
textDirection: TextDirection.ltr,
30-
style: TextStyle(
31+
style: const TextStyle(
3132
fontWeight: FontWeight.w500, color: Colors.red, fontSize: 14.0),
3233
),
33-
SizedBox(height: 30),
34+
const SizedBox(height: 30),
3435
// Center(
3536
// child: CircularProgressIndicator()
3637
// ),

0 commit comments

Comments
 (0)