Skip to content

Commit

Permalink
updating website
Browse files Browse the repository at this point in the history
  • Loading branch information
rodydavis committed Apr 13, 2024
1 parent efaf4fc commit a09a832
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
16 changes: 16 additions & 0 deletions website/src/content/docs/flutter/value-notifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ title: ValueNotifier
description: Easy conversion between ValueNotifier and Signal
---

## SignalValueNotifier

To create a `ValueNotifier` that is also a `Signal`:

```dart
final signal = SignalValueNotifier<int>(10);
// or
final signal = signalValueNotifier<int>(10);
expect(signal.value, 10);
expect(signal is Signal<int>, true);
expect(signal is ValueNotifier<int>, true);
```

> Setting the value on the signal will update the notifier and vice versa.
## Signal from ValueNotifier

To create a mutable signal from a `ValueNotifier`, use the `toSignal` extension:
Expand Down
44 changes: 44 additions & 0 deletions website/src/content/docs/guides/bi-directional-data-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Bi-directional Data Flow
description: By default, Signals are uni-directional but can be used in a bi-directional way if needed.
---

By default, Signals are uni-directional but can be used in a bi-directional way if needed.

> Warning: Bi-directional data flow should only be used when necessary as it can lead to infinite loops if not used correctly.
Consider the following example:

```dart
final a = signal(0);
final b = signal(0);
effect(() {
b.value = a.value + 1;
});
effect(() {
a.value = b.value + 1;
});
```

In this example, `a` and `b` are two signals that are dependent on each other. When `a` changes, `b` should update, and when `b` changes, `a` should update.

This however can lead to an infinite loop and will throw a `EffectCycleDetectionError`. To prevent this, you can use the `untracked` method to prevent the signal from updating itself.

```dart
final a = signal(0);
final b = signal(0);
effect(() {
b.value = untracked(() => a.value + 1);
});
effect(() {
a.value = untracked(() => b.value + 1);
});
```

This will prevent the infinite loop and allow the signals to update each other without causing an error.

Signals are synchronous and will update immediately when the value is set. This means that the value will be updated before the next effect is run. This allows you to create bi-directional data flow in a predictable way.
25 changes: 21 additions & 4 deletions website/src/content/docs/guides/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ import 'package:get_it/get_it.dart';
import 'package:flutter/material.dart';
void main() {
final getIt = GetIt.instance;
getIt.registerSingleton<Signal<int>>(signal(0));
GetIt.I.registerSingleton<Signal<int>>(signal(0));
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counter = GetIt.instance<Signal<int>>();
final counter = GetIt.I.get<Signal<int>>();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -130,10 +129,12 @@ class MyApp extends ConsumerWidget {
}
```

## InheritedWidget (Experimental)
## InheritedWidget

InheritedWidget is a simple built in way to provide objects to your widgets. This comes at the cost of storing a single signal per type.

> Note: This is a new feature added in version 5.0.0 and is still experimental.
```dart
import 'package:signals/signals_flutter.dart';
import 'package:signals/signals_flutter_extended.dart';
Expand Down Expand Up @@ -172,6 +173,22 @@ class MyApp extends StatelessWidget {
}
```

If you want to define multiple signals with the same type, then you will need to create custom classes for the container.

```dart
class Counter extends Signal<int> {
Counter(int value) : super(value);
}
...
home: SignalProvider<Counter>(
instance: Counter(0),
child: MyApp(),
),
...
final counter = SignalProvider.of<Counter>(context);
counter.value++;
```

## Zones

Zones are another built in way to provide objects to your application via Dart [Zones](https://dart.dev/articles/archive/zones).
Expand Down

0 comments on commit a09a832

Please sign in to comment.