Skip to content

Instantly share code, notes, and snippets.

View terryl1900's full-sized avatar

Xianzhe Liang terryl1900

View GitHub Profile
@terryl1900
terryl1900 / main.dart
Created July 26, 2022 03:54
Counter app with a demo reactive state management solution.
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 = [];
@terryl1900
terryl1900 / main.dart
Last active July 26, 2022 15:36
Counter app with a demo cubit implementation
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
@terryl1900
terryl1900 / main.dart
Last active July 26, 2022 01:06
Counter app with provider
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
// counter.dart
class CountNotifier extends ChangeNotifier {
int count = 0;
void increment() {
count += 1;
@terryl1900
terryl1900 / main.dart
Last active July 25, 2022 02:13
Counter app with InheritedNotifier
import 'package:flutter/material.dart';
// counter.dart
class CountNotifier extends ChangeNotifier {
int count = 0;
void increment() {
count += 1;
notifyListeners();
@terryl1900
terryl1900 / main.dart
Last active July 21, 2022 23:29
Counter app with stream
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;
@terryl1900
terryl1900 / readme.md
Last active July 21, 2022 23:42
Inside popular Flutter state management libraries

| Propagate state | Library | Provide data | State is stored in | Can combine states | Easy to learn | | ------------------------- | ------------------------------------------------------------------------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@terryl1900
terryl1900 / main.dart
Created June 12, 2022 17:41
Build Creator - Step 2 - News Example
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.
@terryl1900
terryl1900 / main.dart
Last active June 12, 2022 02:20
Build Creator - Step 2 - Weather
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));
@terryl1900
terryl1900 / main.dart
Last active June 12, 2022 02:20
Build Creator - Step 2 - Counter API
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.
@terryl1900
terryl1900 / main.dart
Last active June 12, 2022 02:20
Build Creator - Step 2
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()));