Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Dart constructor

Tags:

flutter

dart

In the flutter Examples page, there is a project entitled "Sending Data to a new screen". I have question reguarding the constructor on line 65.

Sending Data to a new screen

  // In the constructor, require a Todo   DetailScreen({Key key, @required this.todo}) : super(key: key); 

What is the super(key: key)? Could I get explanation of the entire line please. The Code is here....

import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart';  class Todo {   final String title;   final String description;    Todo(this.title, this.description); }  void main() {   runApp(MaterialApp(     title: 'Passing Data',     home: TodosScreen(       todos: List.generate(         20,             (i) => Todo(           'Todo $i',           'A description of what needs to be done for Todo $i',         ),       ),     ),   )); }  class TodosScreen extends StatelessWidget {   final List<Todo> todos;    TodosScreen({Key key, @required this.todos}) : super(key: key);    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text('Todos'),       ),       body: ListView.builder(         itemCount: todos.length,         itemBuilder: (context, index) {           return ListTile(             title: Text(todos[index].title),             // When a user taps on the ListTile, navigate to the DetailScreen.             // Notice that we're not only creating a DetailScreen, we're             // also passing the current todo through to it!             onTap: () {               Navigator.push(                 context,                 MaterialPageRoute(                   builder: (context) => DetailScreen(todo: todos[index]),                 ),               );             },           );         },       ),     );   } }  class DetailScreen extends StatelessWidget {   // Declare a field that holds the Todo   final Todo todo;    // In the constructor, require a Todo   DetailScreen({Key key, @required this.todo}) : super(key: key);    @override   Widget build(BuildContext context) {     // Use the Todo to create our UI     return Scaffold(       appBar: AppBar(         title: Text("${todo.title}"),       ),       body: Padding(         padding: EdgeInsets.all(16.0),         child: Text('${todo.description}'),       ),     );   } } 
like image 961
IrishGringo Avatar asked Jun 24 '18 19:06

IrishGringo


People also ask

What is constructor in Dart Flutter?

Constructor is a special method of Dart class which is automatically called when the object is created. The constructor is like a function with/without parameter but it doesn't have a return type.

How many named constructors can you have in a dart Flutter class?

You can only have one unnamed constructor, but you can have any number of additional named constructors in Flutter. By using named constructor you can create multiple constructors in the same class. Each constructor will have a unique name. So that you can identify each of them.

Does DART support multiple constructors?

Named constructors in Dart It is called named constructors. Giving your constructors different names allows your class to have many constructors and also to better represent their use cases outside of the class.


1 Answers

The constructor has two named parameters.
Named parameters are optional by default.
@required is an annotation recognized by the Dart analyzer and produces a warning if not passed when invoked at build time (it has no effect at run time).

: starts the "initializer list", a comma sparated list of expressions executed before the constructors of the super classes and therefore also before the contructors body.
It is often used to check parameter values using assertions and to initialize final fields with calculated values.
A limitation is, that expressions can't read-access this. (implicitely or explicitely) because the object initialization is not completed before the super constructors are executed.

The last element in the initializer is an implicit call to the default constructor of the super class if omitted, or the call to a specific constructor of the current class or the super class if given.

In the example in your question the key parameter passed to the constructor is forwarded to the named parameter key of the unnamed constructor of the super class.

like image 121
Günter Zöchbauer Avatar answered Oct 04 '22 10:10

Günter Zöchbauer