dimanche 24 février 2019

TESTING: No MaterialLocalizations found. AppBar widgets require MaterialLocalizations to be provided

When testing a fullscreen widget which contains a Scaffold(), I get this error:

No MaterialLocalizations found.
AppBar widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.

I was trying to inject Localizations widget, but the error remains the same. Any ideas how to proceed testing?

testWidgets('MiniScaffold', (WidgetTester tester) async {
    await tester.pumpWidget(Wrap(child: MiniScaffold()));
    expect(find.text('Hello'), findsOneWidget);
    await tester.pumpWidget(Container());
  });

class MiniScaffold extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Hello'),
          actions: <Widget>[],
        ),
        body: Container());
  }
}

class Wrap extends StatelessWidget {
  final Widget child;

  const Wrap({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
        data: new MediaQueryData(),
        child: Localizations(
            locale: Locale('en-US'),
            delegates: [
              DefaultMaterialLocalizations.delegate,
              DefaultWidgetsLocalizations.delegate,
            ],
            child: Directionality(
              textDirection: TextDirection.ltr,
              child: this.child,
            )));
  }
}

Aucun commentaire:

Enregistrer un commentaire