flutter_solidart는 SolidJS의 반응형 프로그래밍 모델에서 영감을 받은 플러터용 가벼운 상태 관리 솔루션입니다. 상태 변경에 영향을 받는 UI 부분만 재구성되는 세밀한 반응성 기능을 제공하여 성능을 향상시킵니다. 반응형 변수와 파생 값을 사용해 봉쇄 코드 없이 상태 업데이트를 간소화합니다. 플러터 위젯 트리와 원활하게 통합되며 모든 주요 플랫폼을 지원합니다. 전통적인 상태 관리 접근 방식의 단순하고 효율적인 대안을 찾는 개발자를 위해 설계되었습니다.
사용 사례
폼 내 실시간 데이터 업데이트
최소한의 재구성으로 효율적인 UI 업데이트
소규모에서 중규모 앱에서의 간단한 반응형 로직
여러 위젯 간 상태 공유
반응형 대시보드 만들기
복잡한 설정 없이 가벼운 상태 관리
주요 기능
세밀한 반응성
최소한의 보일러플레이트
반응형 변수 및 파생 상태
자동 종속성 추적
모든 플러터 플랫폼을 지원합니다
기존 위젯과의 간편한 통합
적합한 대상
반응형 프로그래밍에 익숙한 개발자
빠른 UI 업데이트가 필요한 프로젝트
무거운 상태 관리 프레임워크를 피하는 앱
복잡성보다는 단순성을 선호하는 팀
빠른 UI 상호작용 프로토타이핑
작은부터 중간 규모의 Flutter 애플리케이션
고려 사항
복잡한 상태를 가진 대규모 앱에는 적합하지 않음
성숙한 대안에 비해 문서화가 제한적
반응형 패턴에 익숙하지 않은 사용자에게는 학습 곡선이 높다
인기 있는 상태 관리자보다 커뮤니티 지원이 적음
부작용을 신중하게 다뤄야 할 수 있습니다
자주 발생하지만 국지적인 상태 변경이 있는 앱에 가장 적합
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
A simple state-management library inspired by SolidJS.
The objectives of this project are:
Being simple and easy to learn
Fits well with the framework's good practices
Do not have a single global state, but multiple states only in the most appropriate places
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 sourcefinal userId = Signal(1);
// The fetcherFuture<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 resourcefinal 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:
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.