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}'), ), ); } }
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With