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},
  );
}
copied to clipboard

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'),
          ),
        ),
      ),
    );
  }
}
copied to clipboard

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},
  );
}
copied to clipboard
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'),
          ),
        ),
      ),
    );
  }
}
copied to clipboard
9
likes
90
points
96
downloads

Publisher

unverified uploader

Weekly Downloads

2024.07.08 - 2025.01.20

A subset of WidgetsFlutterBinding specifically for ServicesBinding.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_services_binding