Closed saibotma closed 7 months ago
Thanks for the detailed bug report! I was able to reproduce it in the test below.
It is a known issue, caused by the Localizations
widget. It loads the Locale asynchronous (calling LocalizationsDelegate.load(Locale)
). While loading, it returns a SizedBox()
instead of the app for roughly 1 frame.
// class Localizations, flutter/lib/src/widgets/localizations.dart
@override
Widget build(BuildContext context) {
if (_locale == null) {
return const SizedBox.shrink(); // Shown for one frame
}
return Semantics(
textDirection: _textDirection,
child: _LocalizationsScope(
key: _localizedResourcesScopeKey,
locale: _locale!,
localizationsState: this,
typeToResources: _typeToResources,
child: Directionality(
textDirection: _textDirection,
child: widget.child!, // <-- this is your app
),
),
);
}
Because your app is removed for one frame, it loses its state.
The workaround is rather straight forward. Change your load method to return a SynchronousFuture
. Remove the async
keyword.
// Your app will lose state
@override
- Future<WiredashLocalizations> load(Locale locale) async {
+ Future<WiredashLocalizations> load(Locale locale) {
switch (locale.languageCode) {
case 'en':
- return _EnOverrides();
+ return SynchronousFuture(_EnOverrides());
default:
throw "Unsupported locale $locale";
}
}
Wiredash should catch this case, and show a warning when load
does not return a SynchronousFuture
.
Implementations that really load the localizations asynchronously, can asynchronously create a synchronous version of their LocalizationsDelegate
instead.
Unfortunately, Wiredash can not yet get rid of the Localizations
widget, or replace it with its own implementation. Wiredash still depends on a hand-full of material widgets (like TextFormField
) which require the Localizations
widget.
Thanks for the quick response & implementing the warning.
Describe the bug Causes child widgets to lose state when localization delegate is added. Steps to reproduce:
TestWidget.inistState
got called, which is not expected.options
parameter in line 26Example
Expand
```dart import 'package:flutter/material.dart'; import 'package:wiredash/wiredash.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override StateWiredash SDK Info Version 2.1.0
Flutter SDK Version: 3.19.1