telescope 1.3.0 copy "telescope: ^1.3.0" to clipboard
telescope: ^1.3.0 copied to clipboard

A simple and easy to use state manager for flutter based on observable pattern with useful out of the box features.

Telescope #


Just another state manager for flutter based on observable👀 design pattern.

Telescope🔭 is

  1. Easy to learn📖
    1. You can learn it in 5-10 min by reading README.
    2. Also see examples.
  2. Flexible🌊
    1. Can be used beside other state managers👫.
    2. It lets you do it in your way💪.
  3. Make it harder to make bugs🪲⛔.
    1. With separation of concerns🙌.
    2. No setState() needed🙅.
  4. Fast⚡ (just rebuild widgets that need to rebuild).
  5. Lightweight🐥 (less then 900KB)
  6. Feature rich♥️
    1. Save states on disk and load when needed.
    2. Telescopes can watch each other with dependsOn() constructor.
    3. Depends on can be async
    4. Caching ability. (coming soon)
    5. Request a feature here.

Installation #

flutter pub add telescope

Import: #

import 'package:telescope/telescope.dart';

How to use #

In 3 easy steps.

1. Make a telescope : #

var textValue = Telescope("default value");

2. Watch(this): #

Put this in middle of you widget build function.
You can watch one telescope instance from multiple widget => example.

@override
Widget build(BuildContext context) {
  return Material(
      child: SafeArea(
          child: Container(
            child: Column(children: [
              // watch like this ('this' is State that will automatically rebuild on data change)
              Text(textValue.watch(this)), 
              Text(textValue.watch(this)),
              Text(textValue.watch(this).length.toString()),
            ],),
          )
      )
  );
}

3. Update value: #

You can update telescope.value from anywhere from your code:

onTap: (){
  textValue.value += "a";
}

Boom: #

And state will get update automatically without calling setState


Other features: #

Depends on: #

Telescopes can be depended on other telescopes

var height = Telescope(186);
var weight = Telescope(72);

var bmi = Telescope.dependsOn([height,weight], () {
  return weight.value / ((height.value/100) * (height.value/100));
});

var showingText  = Telescope.dependsOn([bmi], () {
  return "weight is ${weight.value} and height is ${height.value} so bmi will be ${bmi.value.toString().substring(0,5)}";
});

So when ever height or weight value get changes, the bmi will calculate itself because it depends on height and weight.
And showingText will calculate itself too, because it depends on bmi.

Async way:

var bmi = Telescope.dependsOnAsync(0, [height, weight], () async {
  return await calculateBMI(height.value, weight.value);
});

Observable on loading state:

This will make isCalculatingBMI true on loading and false when loaded, you may need this to show loading animation.

var isCalculatingBMI = Telescope<bool>(false);
var bmi = Telescope.dependsOnAsync(0, [height, weight], () async {
   return await calculateBMI(height.value, weight.value);
}, isCalculating: isCalculatingBMI);

Save On Disk #

You can save telescope data on disk easily like this:

var height = Telescope.saveOnDiskForBuiltInType(187, "bmi_height_input");

So if user close the app and open it again it will load last value of telescope for You.

TelescopeList #

Telescope implementation for list

  • can be dependent
  • can save on disk
var items = TelescopeList(["ab", "abb", "bc", "bcc" , "c"]);

Save non built in values on disk #

You need to implement OnDiskSaveAbility for your object:
For example you have Human class:

class Human{
   int height;
   int weight;
   Human(this.height,this.weight);

   @override
   int get hashCode => height*weight;
}

Then you need to make other class like this for Human:

class HumanOnDiskAbility implements OnDiskSaveAbility<Human>{
   @override
   Human parseOnDiskString(String data) {
      var sp = data.split(":");
      return Human(int.parse(sp[0]), int.parse(sp[1]));
   }

   @override
   String toOnDiskString(Human instance) => "${instance.height}:${instance.weight}";
}

And pass instance of HumanOnDiskAbility to Telescope:

var human = Telescope.saveOnDiskForNonBuiltInType(
        Human(187, 72),
        "human_for_bmi",
        HumanOnDiskAbility()
);

Telescope will use 'parseOnDiskString' and 'toOnDiskString' to serialize and deserialize your object.

This method also can use in TelescopeList in same way.

Examples #

examples.

License #

MIT

7
likes
0
pub points
16%
popularity

Publisher

unverified uploader

A simple and easy to use state manager for flutter based on observable pattern with useful out of the box features.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, shared_preferences

More

Packages that depend on telescope