| Propagate state | Library | Provide data | State is stored in | Can combine states | Easy to learn | | ------------------------- | ------------------------------------------------------------------------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// A counter app with a basic reactive state management. It works similar to | |
// libraries like riverpod, mobx, getx. | |
// Obviously, this is a over simplified version, but hope it shows the gist. | |
// library.dart | |
/// A stack tracks Rx variables being recreated. | |
List<Rx> _creating = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
// A counter app with a demo cubit implementation. The real cubit works | |
// slightly different, but this shows the gist. | |
// library.dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
// counter.dart | |
class CountNotifier extends ChangeNotifier { | |
int count = 0; | |
void increment() { | |
count += 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// counter.dart | |
class CountNotifier extends ChangeNotifier { | |
int count = 0; | |
void increment() { | |
count += 1; | |
notifyListeners(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
// A counter app using Stream to manage state. | |
// counter.dart | |
int _count = 0; | |
final _controller = StreamController<int>()..add(_count); | |
Stream<int> counter() => _controller.stream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// Simple news app with infinite list of news. It shows combining creators | |
// for loading indicator and fetching data with pagination. | |
// repo.dart | |
// Pretend calling a backend service to get news. | |
const count = 10; // Item count per page. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// Simple weather app shows splitting backend/logic/ui code, building logic | |
// with Creator for both sync and async behavior. | |
// repo.dart | |
// Pretend calling a backend service to get fahrenheit temperature. | |
Future<int> getFahrenheit(String city) async { | |
await Future.delayed(const Duration(milliseconds: 100)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// A counter app shows how to expose state mutate APIs. Simple and no magic. | |
// counter_logic.dart | |
// Hide the creator with file level private variable. | |
final _counter = Creator((ref, self) => 0); | |
// Expose the state related APIs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
// A counter app shows basic Creator/Watcher usage. | |
// Creator creates a stream of data. | |
final counter = Creator((ref, self) => 0); | |
void main() { | |
// Wrap the app with a creator graph. | |
runApp(CreatorGraph(child: const MyApp())); |
NewerOlder