Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`nameof` operator in flutter

Tags:

flutter

dart

There is nameof operator in C#, it allows to get property name at compile time:

var name = nameof(User.email);
Console.WriteLine(name);
//Prints: email

It is not possible to use reflection in flutter and I do not want to hardcode names of properties i.e. to be used for querying SQLite tables. Is there any workaround?

***Currently I'm using built_value library.

like image 246
kemsky Avatar asked Dec 11 '18 19:12

kemsky


People also ask

What is Nameof operator?

The nameof operator accepts the name of code elements and returns a string literal of the same element. The parameters that the nameof operator can take can be a class name and all its members like methods, variables and constants and returns the string literal.

How do you variable a String in flutter?

If you are attempting to put a String variable inside another static String you can do the following for simple variables: String firstString = 'sentence'; String secondString = 'This is a $firstString'; Which will output This is a sentence .

How do you define a String in flutter?

Strings are mainly used to represent text. A character may be represented by multiple code points, each code point consisting of one or two code units. For example, the Papua New Guinea flag character requires four code units to represent two code points, but should be treated like a single character: "🇵🇬".

What is Nameof operator in C#?

C# NameOf operator is used to get name of a variable, class or method. It returns a simple string as a result. In error prone code, it is useful to capture a method name, in which error occurred. We can use it for logging, validating parameters, checking events etc.


2 Answers

For the archives, I guess, this isn't possible as Dart doesn't store the names of variables after compiling, and as you mentioned, Flutter doesn't support reflection.

But you can still hardcode responsibly by grouping your properties as part of the object that they belong to, like with JSON:

class User {
  final String email;
  final String name;
  const User({required this.email, required this.name});

  Map toJson() => {
    "email": email,
    "name": name,
  };
}

Instead of remembering to type out "email" and "name" whenever you use User, just call User.toJson(). Then, when you want to rename your variables, you can use your IDE's "rename all", or just skim over your User class to quickly change all of the names without missing any.

like image 58
Levi Lesches Avatar answered Sep 25 '22 09:09

Levi Lesches


You can implement your own nameOf function:

String? nameOf(dynamic o) {
  if (o == null) return "null";

  try {
    if (o is List) {
      var first = o.length > 0 ? o[0] : null;
      if (first != null) {
        var elementType = nameOf(first)!;
        Log.debug("nameOf: List<$elementType>");
        if (!isMinified(elementType))
          return "List<$elementType>";
      }
    } else {
      Function? getTypeName = o.getTypeName;
      if (getTypeName != null) return getTypeName();
    }
  } catch (e) {
    Log.debug("ignored nameOf error: $e, falling back to o.runtimeType: ${o.runtimeType}");
  }

  return o.runtimeType.toString();
}

bool isMinified(String type) => type.startsWith("minified:");
like image 23
Husam Ebish Avatar answered Sep 24 '22 09:09

Husam Ebish