Design and Implementation of Fair 2.0 Logic Syntax Sugar
This article introduces Fair 2.0, a Flutter dynamic framework that adds logic‑level syntactic sugar to simplify mixed layout‑logic scenarios, explains its dual‑state consistency design, presents three solution options with the chosen implementation, and provides code examples for registration and usage.
Fair 2.0 is a dynamic framework for Flutter that enables runtime widget updates via a compiler; version 2.0 adds logic‑level syntactic sugar to improve layout‑logic mixing.
Syntax sugar is defined as language constructs that do not add functionality but improve readability; Fair's sugar aims to simplify mixed layout‑logic scenarios.
Three design options were considered: (1) handling logic entirely on the JS side, (2) delegating simple conditional logic to Dart while keeping layout generation on the DSL side, and (3) custom designs. Option 2 was chosen.
The architecture ensures “dual‑state” consistency so that the same code runs both in native Flutter and in the dynamic runtime, with a registration mechanism that exposes sugar methods to the Fair engine.
Example static implementation registers a Sugar.map method in Dart; dynamic registration is performed in FairApp setup, exposing the method via a provider map.
class Sugar {
static List
map
(List
data, {T Function(E item) builder}) {
return data.mapEach((index, item) => builder(item));
}
}Usage in a Flutter widget demonstrates calling Sugar.map(_list, builder: (item) { … }) to generate a list of child widgets.
class _State extends State
{
var _list = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 100];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: Sugar.map(_list, builder: (item) {
return Container(
child: Row(...),
);
}),
));
}
}The DSL generated from the sugar call contains a className: "Sugar.map" node with parameters pointing to the source list and a builder description, which the runtime iterates to produce the final widget tree.
{
"body": {
"className": "ListView",
"na": {
"children": {
"className": "Sugar.map",
"pa": ["^(_list)"],
"na": {
"builder": { ... }
}
}
}
},
"methodMap": {}
}Currently Fair includes five built‑in sugars (if, ifRange, Map, etc.) and plans to expand based on community contributions.
58 Tech
Official tech channel of 58, a platform for tech innovation, sharing, and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.