Open ghost opened 3 years ago
@ValentinVignalNovade i checked this code, same bug...
@aissat check this test please
I found the post on StackOverflow and landed here. It's most likely due to how the asset bundle loads the translation file:
// from flutter/lib/src/services/asset_bundle.dart
Future<String> loadString(String key, { bool cache = true }) async {
final ByteData data = await load(key);
// Note: data has a non-nullable type, but might be null when running with
// weak checking, so we need to null check it anyway (and ignore the warning
// that the null-handling logic is dead code).
if (data == null)
throw FlutterError('Unable to load asset: $key'); // ignore: dead_code
// 50 KB of data should take 2-3 ms to parse on a Moto G4, and about 400 μs
// on a Pixel 4.
if (data.lengthInBytes < 50 * 1024) { // <-------------------- this
return utf8.decode(data.buffer.asUint8List());
}
// For strings larger than 50 KB, run the computation in an isolate to
// avoid causing main thread jank.
return compute(_utf8decode, data, debugLabel: 'UTF8 decode for "$key"');
}
As a workaround, if you add this line before the tester.pumpAndSettle(), it works on my machine at least:
await Future.delayed(const Duration(milliseconds: 150), () {});
Link to the full answer here
I also had this issue. The way i solved was using CodeGeneration.
In the end, I fixed it by adding a file test/flutter_test_config.dart
. I got the inspiration from this issue.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:easy_localization/src/localization.dart';
import 'package:easy_localization/src/translations.dart';
import 'package:flutter/widgets.dart';
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
final content = await File('assets/lang/sv.json').readAsString(); // <- Or `ru.json`
final data = jsonDecode(content) as Map<String, dynamic>;
// easy_localization works with a singleton instance internally. We abuse
// this fact in tests and just let it load the English translations.
// Therefore we don't need to deal with any wrapper widgets and
// waiting/pumping in our widget tests.
Localization.load(
const Locale('en'),
translations: Translations(data),
);
await testMain();
}
I'm using easy_localization in a flutter project. I need to write some widget tests.
But it looks like that when the
.json
file with the translations is too big, theMaterialApp
never builds itschild
and therefore, I cannot test my widgets.Here is the architecture of my small reproducible project:
Here is my
test_test.dart
file:sv.json
(a small file):ru.json
(a big file):In my test, I have 2
print
s which should respectively printbuilder1
andbuilder2
.This works well when I use
Locale('sv')
in my test:But when I use
Locale('ru')
,MaterialApp
doesn't build its child and I don't get the printbuilder2
:How can I fix this?