I saw this reddit post yesterday. This is obviously a bad take. Not only they are arguing from ignorance but they are also being dishonest about the problems a new learner would come across when adopting Flutter. However, there is a little kernel of truth buried in the whole post which I think got overlooked by most of the replies there. Flutter is not verbose to learn, but Flutter is too verbose to write. Let me explain what I mean.
Hello world! π
Let's write a simple Hello World app. Lets first write this in Dart for Flutter
class HelloWorld extends StatelessWidget {
final String name;
HelloWorld({required this.name});
@override Widget build(BuildContext context) {
return Text('Hello $name');
}
}
Simple enough. Let's write the same written in Swift for SwiftUI
struct HelloWorld: View {
var name: String
var body: some View {
Text("Hello \(name)")
}
}
Oh boy. Let's write in Kotlin for Jetpack Compose
@Composable
fun HelloWorld(name: String) {
Text("Hello $name") }
}
and lets throw JS for React too
function HelloWorld(props) {
return <div>Hello {props.name}</div>;
}
This was just a simple hello world, and things are already looking bad for Flutter. Why?
π© Higher LOC ; We are immediately accumulating boilerplate
π© Inheritance Hell; Needing to extend a class (StatelessWidget, tbh also true for View on SwiftUI). Inheritance Is evil, and from what I learned over the last couple years - always prefer composition over inheritance. But we are stuck with inheritance in Flutter (and SwiftUI)
I understand why these need to be this way to set up the widget tree. But does it need to be? Imagine writing this in Dart instead
Widget HelloWorld(BuildContext context, String name) {
return Text('Hello $name');
}
This is already doable with this plugin, but this comes with a cost (of code generation). Either-way, how is this better?
β
Lower LOC ; No longer we are accumulating boilerplate
β
Inheritance Hell is abstracted away; Sure, inheritance is still there, Flutter is still Flutter, but you don't need to care about inheritance when you write your widgets.
Why can't this be the code we write? Personally, I think flutter team needs to propose an alternative functional widget approach that compete with the traditional class approach. There was this issue that was raised (and now closed), but i think such an alternative DSL can effectively address the verboseness of Flutter.
It gets more harder π€―
Lets add some state into our widgets. Lets first write this in Dart for Flutter
class CounterWidget extends StatefulWidget {
@override CounterState createState() => CounterState();
}
class CounterState extends State<CounterWidget> {
int count = 0;
@override Widget build(BuildContext context) {
return RaisedButton(
onPressed: () => setState(() => count += 1),
child: Text("Count: $count"),
);
}
}
Let's write the same written in Swift for SwiftUI
struct Counter: View {
@State private var count = 0
var body: some View {
Button(action: {
count += 1
}) {
Text("Count: \(count)")
}
}
}
In Kotlin for Jetpack Compose
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = { count += 1 }) {
Text("Count: $count")
}
}
and lets throw in JS for React too
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
Things are looking worse for Flutter. Why?
π© Backward Inheritance; I never understood the reasoning behind this backward inheritance structure where you need to subclass a State to get a Widget. When I first learned Flutter; I didn't question this, but reflecting back - this makes zero sense.
π© State Coupled with Widget; Because of this backward inheritance structure, your State can not be fully decoupled from the Widget tree (because your State need to know about the Widget). Because of this bad API design, you often need to duplicate State when you need to reuse the same State across multiple Widgets. This is not a problem other frameworks.
But does this need to be this bad? Building up from the previous example; imagine writing this in Dart instead
Widget CounterWidget(BuildContext context, CounterState state) {
return RaisedButton(
onPressed: () => state.setState(() => count += 1),
child: Text("Count: $count"),
);
}
How is this better?
β
No more backward inheritance ; State is State, a Widget is a Widget
β
No more coupling; State is just a holding class for your data, it doesn't need to know about the UI (Widget)
Lets Mix It Up πͺπͺπͺ
Lets make our HelloWorldWidget to make it look a little nicer. In Dart for Flutter
class HelloWorldWidget extends StatelessWidget {
final String name;
HelloWorld({required this.name});
@override Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16.0),
color: Colors.blue,
child: Text('Hello $name!') );
}
}
}
Let's write the same written in Swift for SwiftUI
struct HelloWorld: View {
var name: String
var body: some View {
Text("Hello \(name)!")
.padding(EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16))
.background(Color.blue)
}
}
In Kotlin for Jetpack Compose
@Composable
fun HelloWorld(name: String) {
Text(
text = "Hello $name!",
modifier = Modifier
.padding(16.dp)
.background(Color.Blue)
)
}
In JS for React
function HelloWorld(props) {
const style = {
padding: "16px",
backgroundColor: "blue",
color: "white",
};
return (<div style={style}>
Hello {props.name}!
</div>);
}
Why this approach is a pitfall for Flutter
Can we do better? Building up from the previous example; imagine writing this in Dart instead
Widget HelloWorld(BuildContext context, String name) {
return Text('Hello $name')
.padding(16)
.background(Colors.blue);
}
}
How is this better?
Lif(s)t it up π
Let's make a list of 'Hello's. Lets first write this in Dart for Flutter
class HelloMyPeople extends StatelessWidget {
final List<People> people;
HelloMyPeople({required this.people});
@override Widget build(BuildContext context) {
return ListView.builder(
itemCount: people.length,
itemBuilder: (context, index) {
final person = people[index];
return ListTile(
key: Key(person.id),
title: Text('Hello ${person.name}!'),
);
}
);
}
}
Let's write the same written in Swift for SwiftUI
struct HelloMyPeople: View {
let people: [Person]
var body: some View {
List(people) { person in
Text("Hello \(person.name)!")
}
}
}
In Kotlin for Jetpack Compose
@Composable
fun HelloMyPeople(people: List<Person>) {
LazyColumn {
items(items, key = { person -> person.id }) { person ->
Text("Hello ${person.name}!")
}
}
}
And JS for React
function HelloMyPeople({ people }) {
return ( <ul> {
people.map((people) => ( <li key={people.id}>Hello {people.name}</li> ))
} </ul> );
}
Not too bad for Flutter. Why?
Can we do better? Building up from the previous example; imagine writing this in Dart instead
Widget HelloMyPeople(BuildContext context, List<People> people) {
return ListView(
items: people,
builder: (context, person) {
return ListTile(
key: Key(person.id),
title: Text('Hello ${person.name}!'),
);
),
);
}
How is this better?
We can do better π
I love flutter. It made me fall in love with programming and I'm forever grateful for that. But to claim it is not that verbose - as demonstrated - is just not true. It is definitely less verbose than UIKit and XML but it has a long way to go compared to the modern native frameworks.
Before you grab your pitchforks π±
Here's a little context as to where I'm coming from. I joined a small startup during COVID and got to work alongside a small flutter team building an awesome app. It was genuinely a fun project and I learned a lot. Unfortunately the company went under and I was laid off, and all opportunities in my market were looking for senior positions for which I was not qualified for. Out of desperation, I started to learn react native and native development in general - and was genuinely surprised how native platforms have caught up over the years. I always assumed native development was unnecessarily complex and unapproachable, but after being exposed to it out of desperation - I think it is actually quite good.