flutter_services_binding 0.1.0 copy "flutter_services_binding: ^0.1.0" to clipboard
flutter_services_binding: ^0.1.0 copied to clipboard

discontinued

A subset of WidgetsFlutterBinding specifically for ServicesBinding.

flutter_services_binding #

ci coverage pub package style: very good analysis License: MIT

A subset of WidgetsFlutterBinding specifically for initializing the ServicesBinding.

When executing runApp within a custom Zone with zoneValues derived from the underlying platform we rely on WidgetsFlutterBinding.ensureInitialized():

final token = Object();
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final value = await _getPlatformValue();
  runZoned(
    () => runApp(MyApp()),
    zoneValues: {token: value},
  );
}

This appears to work as expected, however, because WidgetsFlutterBinding also initializes other bindings like GestureBinding the zoneValue is not accessible within onPressed callbacks:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print(Zone.current[token]); // OK
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              print(Zone.current[token]); // NULL
            },
            child: Text('Click Me'),
          ),
        ),
      ),
    );
  }
}

FlutterServicesBinding provides an API to initialize the ServicesBinding and SchedulerBinding independently of the remaining bindings in order to support accessing data from the underlying platform before runApp but without initializing the WidgetsBinding instance within the root Zone.

final token = Object();
void main() async {
  FlutterServicesBinding.ensureInitialized();
  final value = await _getPlatformValue();
  runZoned(
    () => runApp(MyApp()),
    zoneValues: {token: value},
  );
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print(Zone.current[token]); // OK
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              print(Zone.current[token]); // OK
            },
            child: Text('Click Me'),
          ),
        ),
      ),
    );
  }
}
9
likes
90
pub points
77%
popularity

Publisher

unverified uploader

A subset of WidgetsFlutterBinding specifically for ServicesBinding.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_services_binding