# Provider

> Use when setting up providers, consuming state, optimizing rebuilds, using ProxyProvider, or migrating from deprecated providers (Provider package).

- **Type:** Skill
- **Install:** `agentstack add skill-evanca-flutter-ai-rules-provider`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [evanca](https://agentstack.voostack.com/s/evanca)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [evanca](https://github.com/evanca)
- **Source:** https://github.com/evanca/flutter-ai-rules/tree/main/skills/provider

## Install

```sh
agentstack add skill-evanca-flutter-ai-rules-provider
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Provider Skill

This skill defines how to correctly use the `provider` package in Flutter applications.

---

## 1. Provider Types

| Provider | Use for |
|---|---|
| `Provider` | Exposing any immutable value |
| `ChangeNotifierProvider` | Mutable state with `ChangeNotifier` |
| `FutureProvider` | Exposing a `Future` result |
| `StreamProvider` | Exposing a `Stream` |
| `ProxyProvider` / `ChangeNotifierProxyProvider` | Objects that depend on other providers |

---

## 2. Setup

```dart
MultiProvider(
  providers: [
    Provider(create: (_) => Something()),
    ChangeNotifierProvider(create: (_) => MyNotifier()),
    FutureProvider(create: (_) => fetchData(), initialData: ''),
  ],
  child: MyApp(),
)
```

- Use `MultiProvider` to group multiple providers and avoid deeply nested trees.
- `ChangeNotifierProvider` **automatically disposes** the model when it is no longer needed.
- **Never** create a provider's object from variables that can change over time — the object won't update when the variable changes. Use `ProxyProvider` instead.
- If you have 150+ providers, consider mounting them over time (e.g., during a splash screen) rather than all at once to avoid `StackOverflowError`.

---

## 3. Consuming State

Always specify the **generic type** for type safety:

```dart
// Listen and rebuild on change — only valid inside build() or a Provider's update method
final count = context.watch().count;

// Access without listening — use in callbacks, not inside build()
context.read().increment();

// Listen to only part of the state
final count = context.select((m) => m.count);
```

Use `Consumer` or `Selector` widgets when you need fine-grained rebuilds and cannot access a descendant `BuildContext`:

```dart
Consumer(
  builder: (context, model, child) => Text('${model.count}'),
  child: const ExpensiveWidget(), // rebuilt only once
)

Selector(
  selector: (_, model) => model.count,
  builder: (_, count, __) => Text('$count'),
)
```

---

## 4. ProxyProvider

Use `ProxyProvider` or `ChangeNotifierProxyProvider` for objects that depend on other providers or values that can change:

```dart
MultiProvider(
  providers: [
    Provider(create: (_) => Auth()),
    ProxyProvider(
      update: (_, auth, __) => Api(auth.token),
    ),
  ],
)
```

---

## 5. Rules

- Do **not** access providers inside `initState` or constructors — use them in `build`, callbacks, or lifecycle methods where the widget is fully mounted.
- You can use **any object** as state, not just `ChangeNotifier`; use `Provider.value()` with a `StatefulWidget` if needed.

---

## 6. Debugging

Implement `toString` or `DiagnosticableTreeMixin` to improve how your objects appear in Flutter DevTools:

```dart
class MyModel with DiagnosticableTreeMixin {
  final int count;
  MyModel(this.count);

  @override
  String toString() => 'MyModel(count: $count)';
}
```

---

## 7. Migration: ValueListenableProvider

`ValueListenableProvider` is deprecated. Use `Provider` with `ValueListenableBuilder` instead:

```dart
ValueListenableBuilder(
  valueListenable: myValueListenable,
  builder: (context, value, _) {
    return Provider.value(
      value: value,
      child: MyApp(),
    );
  },
)
```

---

## References

- [Provider GitHub Repository](https://github.com/rrousselGit/provider)

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [evanca](https://github.com/evanca)
- **Source:** [evanca/flutter-ai-rules](https://github.com/evanca/flutter-ai-rules)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-evanca-flutter-ai-rules-provider
- Seller: https://agentstack.voostack.com/s/evanca
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
