riverpod_stateful_service 7.0.1 copy "riverpod_stateful_service: ^7.0.1" to clipboard
riverpod_stateful_service: ^7.0.1 copied to clipboard

This package provides a lightweight stateful_service wrapper for Riverpod. See the stateful_service package for more information.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_stateful_service/riverpod_stateful_service.dart';
import 'package:stateful_service/stateful_service.dart';

void main() => runApp(ProviderScope(child: const MyApp()));

class CounterService extends StatefulService<int> {
  CounterService(Ref ref, {super.name = 'default'}) : super(initialState: 0);

  Future<void> increment() => update((state) => state + 1);
}

final StatefulServiceNotifierProviderFamily<CounterService, int, String>
    counterServiceProvider = NotifierProvider.autoDispose.family((name) =>
        StatefulServiceNotifier((ref) => CounterService(ref, name: name)));

final class MyApp extends ConsumerWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      title: 'Stateful Service Riverpod Generator Demo',
      home: Scaffold(
        appBar: AppBar(title: Text('Stateful Service Riverpod Demo')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('You have pushed the button this many times:'),
              Text('${ref.watch(counterServiceProvider('my button').value)}',
                  style: Theme.of(context).textTheme.headlineMedium),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () =>
              ref.read(counterServiceProvider('my button').service).increment(),
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}
2
likes
150
points
798
downloads

Publisher

verified publisherwolverinebeach.net

Weekly Downloads

This package provides a lightweight stateful_service wrapper for Riverpod. See the stateful_service package for more information.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

meta, riverpod, stateful_service

More

Packages that depend on riverpod_stateful_service