Search code examples
classdartconstructorinstance

Unable to make a Dart class object


My Code

class Book {
  String title;
  String author;
  int numOfPages;

  Book(String title, String author, int pages) {
    this.title = title;
    this.author = author;
    this.numOfPages = pages;
  }
}

void main() {
  Book bk = Book("Modern Operating Systems", "S.Tannabeaum", 1250);
  print(bk.title);
}

Hey, I'm pretty a newbie to dart and programming. Here actually I wanted to make a class and it's constructor and three instances within it. And when I wanted to make an object from this class, I caught up with this error!

My code's error message!

  • I think something is wrong with my code, any help would be appreciable:)

Solution

  • There are two problems in your code. First, constructors in Dart has two "phases" where you first initialize the object and then runs the constructor body before returning the object to the caller of the constructor.

    That means that you are here creating a Book object first without setting the three variables. Yes, you are setting these variables later in the constructor body but at that time it is too late.

    The next problem is that if you are not setting value for a variable in Dart it will always default to the value null. With Dart 2.12, we got non-nullable types by default (NNBD) which mean all types in Dart does not allow the value null unless specified. You specify the validity of the null value by typing a ? after the name of the type. E.g. String? allows a variable to point to a String object, or null.

    In this case, we don't need to specify nullable types since the problem is mostly you need to move the initialization of the variables from the constructor body in to initialization phase of the object like this:

    class Book {
      String title;
      String author;
      int numOfPages;
    
      Book(String title, String author, int pages)
          : this.title = title,
            this.author = author,
            this.numOfPages = pages;
    }
    

    The same can be rewritten as the following which is also the recommended way to do it:

    class Book {
      String title;
      String author;
      int numOfPages;
    
      Book(this.title, this.author, this.numOfPages);
    }
    

    Since we are here just directly referring to each field we want to give a value. Dart will then automatically assign the values with the parameters from the constructor.

    If your constructor takes a lot of arguments, it might be more readable to use named arguments. The required keyword here means that we most provide a given named parameter. If not specified, the named argument is optional (which means we most provide a default value or allow null for our parameter to be valid):

    class Book {
      String title;
      String author;
      int numOfPages;
    
      Book({
        required this.title,
        required this.author,
        required this.numOfPages,
      });
    }
    
    void main() {
      final book = Book(
        title: "Modern Operating Systems",
        author: "S.Tannabeaum",
        numOfPages: 1250,
      );
      
      print(book.title); // Modern Operating Systems
    }