Typedef In Flutter

Typedef In Flutter :

Hello everybody friends, in this post, learn to typedef and how to use in dart.

typedef :

  • We declare a function with a name (alias) which we want, i.e. join two string is return String.
  • Typedefs are alias for functions which are object. So, Typedefs are object with an alias =).
  • VoidCallback, a synchronous version of this signature.
  • pageBuilder RoutePageBuilder typedef – just a function that match with the RoutePageBuilder typedef signature.
  • transitionsBuilder RouteTransitionsBuilder typedef – exactly as pageBuilder but matching the RouteTransitionsBuilder signature.
Typedef In Flutter
typedef bool FunctionName(String userName, String password);

bool functionName(String value1, String value2) {
  if (value1 == 'kamlesh' && value2 == 'kamlesh') {
    return true;
  }
  return false;
}

main() {
  var x = functionName;
  var result = getValue(functionName, 'kamlesh', 'kamlesh');
  print(result);
}

bool getValue(FunctionName function, String userName, String password) {
  return function(userName, password);
}


typedef join :

typedef String Join(String first, String second);

main() {
  Join dotJoin = (String first, String second) => dot(first, second);
  print("${joinWith(dotJoin, "Hello", "Typedef!")}");
  print("${joinWith(snakeCase, "Hello", "Typedef!")}");
}

String dot(String first, String second) => "$first.$second";
String snakeCase(String first, String second) => "${first}_$second";
String joinWith(Join join, String first, String second) {
  return join(first, second);
}

Flutter ListView use typedef :

  • items is length. Check the length > 0 if display _buildList() else PlaceholderContent()
  • itemBuilder i.e. ListView one of property itemBuilder.
typedef Widget ItemWidgetBuilder<T>(BuildContext context, T item);
class ListItemsBuilder<T> extends StatelessWidget {
  ListItemsBuilder({this.items, this.itemBuilder});
  final List<T> items;
  final ItemWidgetBuilder<T> itemBuilder;

  @override
  Widget build(BuildContext context) {
    if (items != null) {
      if (items.length > 0) {
        return _buildList();
      } else {
        return PlaceholderContent();
      }
    } else {
      return Center(child: CircularProgressIndicator());
    }
  }

  Widget _buildList() {
    return ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return itemBuilder(context, items[index]);
        });
  }
}

 Widget _buildContent() {
    return ListItemsBuilder<Counter>(
        items: _counters,
        itemBuilder: (context, counter) {
          return Text(counter);
        });
  }

RestAPI call :

typedef bool LoginRequest(String email, String password);

class Account {
  static LoginRequest login(ApiState apiState) =>
      (username, password) => _login(apiState.send, apiState.token, email, password);
}

@immutable
class ApiState {
  final String token;
  final HttpSend send;
  const ApiState(this.send, this.token);
}

typedef Response HttpSend(Request request);

Response send(Request request) {
  return Response("error", 404);
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in