I know that functions are independent from any class and static methods are attached to a class. It looks like both of them achieve the same. This leads me to the following question: What are the advantages and disadvantages of them?

I want to create set of functions for offline PIN configuration like setPin, changePin, verifyPin. Do I have to wrap them inside a class as static methods or can I just create them as functions in a Dart file?


Solution 1: creativecreatorormaybenot

There are no clear "Advantages and Disadvantage" of static methods versus functions.

As you have correctly pointed out, the only difference is that static members are connected to a class.

class A {
  static bool b() => false;
}

bool c() => true;

The only difference here would be that you need to access the static member via A.b and c can be accessed directly.

Static methods are not even inherited, meaning that class B extends A {} will not allow you to use B.b because b is a static member of A.

Having said that, @jamesdlin pointed out an article for writing good Dart design. This article describes that you should avoid creating classes with only static members and calls it bad design, i.e. not idiomatic Dart:

In idiomatic Dart, classes define kinds of objects. A type that is never instantiated is a code smell.

Coming back to your question, you should probably create your functions as top-level functions if they are not part of an object according to idiomatic Dart design based on the article from the Dart team.

However, you might consider changing the way you store your "PIN configuration" because this sounds like it would be ideal to store this information as an object.


Solution 2: Solen Dogan

I totally agree that just using classes with static methods is a bad design decision. It enforces the OO principles but actually does not bring any benefit of OO.

If can not support inheritance and polymorphism then what is the point? Functions on the other hand can bring more flexibility as dart supports standalone functions like javascript etc..

As an example lets look at a flutter class for creating a custom theme .. This example is with a static getter..

import 'package:flutter/material.dart';

class CustomTheme {
  static ThemeData get lightTheme {
    return ThemeData(
      primarySwatch: Colors.purple,
      fontFamily: 'Quicksand',
      errorColor: Colors.purple,
      textTheme: ThemeData.light().textTheme.copyWith(
            headline6: TextStyle(
              fontFamily: 'OpenSans',
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
            button: TextStyle(
              fontFamily: 'OpenSans',
              color: Colors.white,
              fontSize: 18,
            ),
          ),
      accentColor: Colors.indigo,
      appBarTheme: AppBarTheme(
        elevation: 12,
        textTheme: ThemeData.light().textTheme.copyWith(
              headline6: TextStyle(
                fontFamily: 'Quicksand-Bold',
                color: Colors.white,
                fontSize: 18,
              ),
            ),
      ),
    );
  }
}

Think about writing this with a function like this..

import 'package:flutter/material.dart';

ThemeData myCustomLightTheme() {
  final ThemeData _lightTheme = ThemeData.light();

  final _appBarTheme = AppBarTheme(
    elevation: 12,
    textTheme: _lightTheme.textTheme.copyWith(
      headline6: TextStyle(
        fontFamily: 'Quicksand-Bold',
        color: Colors.white,
        fontSize: 18,
      ),
    ),
  );

  final _textTheme = _lightTheme.textTheme.copyWith(
    headline6: TextStyle(
      fontFamily: 'OpenSans',
      fontSize: 18,
      fontWeight: FontWeight.bold,
    ),
    button: TextStyle(
      fontFamily: 'OpenSans',
      color: Colors.white,
      fontSize: 18,
    ),
  );

  return ThemeData(
      primarySwatch: Colors.purple,
      fontFamily: 'Quicksand',
      errorColor: Colors.purple,
      textTheme: _textTheme,
      accentColor: Colors.indigo,
      appBarTheme: _appBarTheme);
}