ilteoood / flutter_i18n

I18n made easy, for Flutter!
MIT License
217 stars 57 forks source link

type 'Widget Function(BuildContext, Widget)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget?)?' from `builder: FlutterI18n.rootAppBuider()` #173

Closed Foldblade closed 3 years ago

Foldblade commented 3 years ago

English is not my native language; please excuse my typing errors.

Hi! Thanks for your package. I use it in my project, it's quite convenient.

Recently I am migirating my project to Null Safety, I'm using:

Flutter 2.5.0-5.0.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 0f465e5b2a (8 days ago) • 2021-08-04 18:59:50 -0400
Engine • revision a0d89b1a54
Tools • Dart 2.14.0 (build 2.14.0-377.0.dev)

and

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter_i18n: ^0.22.4

in my project's pubspec.yaml.

While migirating, I found a problem. When I use:

MaterialApp(
  builder: FlutterI18n.rootAppBuider(),
);

It comes diagnostic below:

The argument type 'Widget Function(BuildContext, Widget)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget?)?'.

I noticed that in issue #167 and related RP #168 , which are related to builder: FlutterI18n.rootAppBuider(),. I'm using the latest release 0.22.4 which contains them.

I tried to solve the problem by changing like below:

static Widget Function(BuildContext, Widget?) rootAppBuilder() {
    Widget appBuilder(BuildContext context, Widget? child) {
      final instance = _retrieveCurrentInstance(context);

      return StreamBuilder<Locale?>(
        initialData: instance?.locale,
        stream: instance?._localeStream.stream,
        builder: (context, snapshot) {
          return Directionality(
            textDirection: _findTextDirection(snapshot.data),
            child: child!,
          );
        },
      );
    }

    return appBuilder;
  }

I opened a PR #174 . Would you please help me check if this modification is proper? Thank you.