搜尋套件、儲存庫與 APP 分類

套件詳情v2.8.6

solidart

一個簡單的 Dart 應用程式狀態管理解決方案,靈感來自 SolidJS

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
6260
喜歡數
80
Pub 分數
150

安裝套件

flutter pub add solidart

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Solidart 是一個輕量級的狀態管理解決方案,適用於 Dart 應用程式,靈感來自 SolidJS。它提供反應式狀態更新,且程式碼樣板極少,可實現高效且可預測的 UI 渲染。透過利用細粒度的反應性,確保狀態變更時僅重建必要的 UI 部分。設計上著重簡潔與效能,Solidart 可無縫整合至跨多個平台的 Flutter 專案中。其 API 直覺易用,讓熟悉反應式程式設計模式的開發者能輕鬆上手。

適用情境

  • Flutter 應用中的即時資料更新
  • 使用最少的重建構建響應式 UI
  • 簡化複雜組件中的狀態邏輯
  • 在多個小部件之間管理共享狀態
  • 建立可擴展且可維護的應用架構

主要特色

  • 細粒度反應性
  • 最小化樣板程式碼
  • 自動依賴追蹤
  • 跨平台支援(Android、iOS、Web、macOS、Windows、Linux)
  • 受 SolidJS 啟發的簡單 API

適合對象

  • 尋找 Provider 或 Riverpod 輕量級替代方案的開發者
  • 需要高性能量狀態更新的專案
  • 熟悉 SolidJS 或響應式程式設計的團隊
  • 頻繁更新 UI 和動態數據的應用
  • 初學者尋找一種直覺的狀態管理方法

使用建議

  • 與成熟的解決方案(如 Riverpod)相比,生態系統有限
  • 較少的社群文件和範例
  • 對於不熟悉反應式概念的人來說,可能需要一定的學習曲線
  • 在沒有仔細架構的情況下,不適合非常大規模的狀態樹
  • 最適合對效能和簡單性要求高於複雜工具的應用程式

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

License: MIT GitHub stars Coverage GitHub issues GitHub pull-requests solidart Pub Version flutter_solidart Pub Version All Contributors

A simple state-management library inspired by SolidJS.

The objectives of this project are:

  1. Being simple and easy to learn
  2. Fits well with the framework's good practices
  3. Do not have a single global state, but multiple states only in the most appropriate places
  4. No code generation

Learning

For a comprehensive and updated documentation go to The Official Documentation

There are 5 main concepts you should be aware:

  1. Signal
  2. Effect
  3. Computed
  4. Resource
  5. Dependency Injection
Signal

Signals are the cornerstone of reactivity in solidart. They contain values that change over time; when you change a signal's value, it automatically updates anything that uses it.

To create a signal, you have to use the Signal class:

final counter = Signal(0);

The argument passed to the class is the initial value, and the return value is the signal.

To retrieve the current value, you can use:

print(counter.value); // prints 0

To change the value, you can use:

// Set the value to 2
counter.value = 2;
// Update the value based on the current value
counter.updateValue((value) => value * 2);
Effect

Signals are trackable values, but they are only one half of the equation. To complement those are observers that can be updated by those trackable values. An effect is one such observer; it runs a side effect that depends on signals.

An effect can be created by using the Effect class. The effect automatically subscribes to any signal and reruns when any of them change. So let's create an Effect that reruns whenever counter changes:

final disposeFn = Effect(() {
    print("The count is now ${counter.value}");
});
Computed

A computed signal is a signal that depends on other signals. To create a computed signal, you have to use the Computed class.

A Computed automatically subscribes to any signal provided and reruns when any of them change.

final count = Signal(0);
final doubleCount = Computed(() => count.value * 2);

Effect(() {
  print('The counter is ${count.value}');
  print('The double counter is ${doubleCount.value}');
});

count
  ..value = 1
  ..value = 2;

// The output will be:
// The counter is 0
// The double counter is 0
// The counter is 1
// The double counter is 2
// The counter is 2
// The double counter is 4
Resource

Resources are special Signals designed specifically to handle Async loading. Their purpose is wrap async values in a way that makes them easy to interact with.

Resources can be driven by a source signal that provides the query to an async data fetcher function that returns a Future.

The contents of the fetcher function can be anything. You can hit typical REST endpoints or GraphQL or anything that generates a future. Resources are not opinionated on the means of loading the data, only that they are driven by futures.

Let's create a Resource:

// The source
final userId = Signal(1);

// The fetcher
Future<String> fetchUser() async {
    final response = await http.get(
      Uri.parse('https://jsonplaceholder.typicode.com/users/${userId.value}/'),
      headers: {'Accept': 'application/json'},
    );
    return response.body;
}

// The resource
final user = Resource(fetchUser, source: userId);

A Resource can also be driven from a Stream instead of a Future, and can be created with Resource.stream(() => stream). In this case you just need to pass the stream field to the Resource class.

If you're using SignalBuilder you can react to the state of the resource:

SignalBuilder(
  builder: (_, __) {
    return user.state.when(
      ready: (data) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ListTile(
              title: Text(data),
              subtitle:
                  Text('refreshing: ${userState.isRefreshing}'),
            ),
            userState.isRefreshing
                ? const CircularProgressIndicator()
                : ElevatedButton(
                    onPressed: user.refresh,
                    child: const Text('Refresh'),
                  ),
          ],
        );
      },
      error: (e, _) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(e.toString()),
            userState.isRefreshing
                ? const CircularProgressIndicator()
                : ElevatedButton(
                    onPressed: user.refresh,
                    child: const Text('Refresh'),
                  ),
          ],
        );
      },
      loading: () {
        return const RepaintBoundary(
          child: CircularProgressIndicator(),
        );
      },
    );
  },
)

The on method forces you to handle all the states of a Resource (ready, error and loading). The are also other convenience methods to handle only specific states.

Dependency Injection

The dependency injection in flutter_solidart is done using the disco package.

This replaced the Solid widget which was used in the previous versions of flutter_solidart.

disco has been built on top of Solid to provide a more powerful and flexible way to handle dependency injection.

Refer to the official disco documentation which contains also examples written with flutter_solidart

DevTools

You can debug your application using the Solidart DevTools extension and filter your signals.

Contributors

Alexandru Mariuti
Alexandru Mariuti

💻 🐛 🚧 💬 👀 📖 ⚠️
manuel-plavsic
manuel-plavsic

💻
Luke Greenwood
Luke Greenwood

📖
9dan
9dan

💻 🐛 📖
Seven Du
Seven Du

💻 🐛 📖
Haresh Gediya
Haresh Gediya

💻

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。