---
title: Counter example
description: Counter example using flutter_solidart
---

# Counter example

A very simple example with a counter.

See the code [here](https://github.com/nank1ro/solidart/tree/main/examples/counter).

In this example we've used a `StatefulWidget` because we must not forget to `dispose()` the created signal.
We've used `counter` as our signal name:
```dart
final counter = Signal(0);
```

Starting it from the `0` value.

> There is no `counter.dispose` as you may have noticed, because by default solidart will auto dispose signals when there are no more listeners.

To display the `counter` value we've used a `SignalBuilder` that rebuild any time the `counter` value changes:
```dart
SignalBuilder(
  builder: (_, __) {
    return Text('${counter.value}');  
  },
)
```

And finally to update our `counter` value we've used a `FloatingActionButton`:
```dart
FloatingActionButton(
  onPressed: () {
    counter.value++;
  },
  child: const Icon(Icons.add),
)
```

## Testing

Testing the counter is pretty simple

```dart
testWidgets('Counter increments', (WidgetTester tester) async {
  // Build our app and trigger a frame.
  await tester.pumpWidget(const MyApp());

  // Verify that our counter starts at 0.
  expect(find.text('0'), findsOneWidget);
  expect(find.text('1'), findsNothing);

  // Tap the '+' icon and trigger a frame.
  await tester.tap(find.byIcon(Icons.add));
  await tester.pump();

  // Verify that our counter has incremented.
  expect(find.text('0'), findsNothing);
  expect(find.text('1'), findsOneWidget);
});
```
