0

I copied the basic structure of this code from a demo, and have adjusted it for my own needs (and reduced it for this question).

But I'm confused by a couple of things. Perhaps a Dart expert can enlighten me...

import 'package:flutter/material.dart';

// Show policy widget - shows 
class ShowPolicy extends StatelessWidget {
  final Policy policy;

  ShowPolicy(this.policy);

  @override
  Widget build(BuildContext context) {
    return Text('Test text ' + policy.riskName + ' ' + policy.policyNumber);
  }
}

// Policy class
class Policy {
  final String riskName;
  final String policyNumber;

  Policy( { this.riskName, this.policyNumber } );
}

// what is this variable doing here?. Shouldn't it be an a class?
List<Policy> allPolicies = [
  Policy(riskName: 'Lilly', policyNumber: 'PY123456-4'),
  Policy(riskName: 'Lilly', policyNumber: 'PY123456-3'),
  Policy(riskName: 'Lilly', policyNumber: 'PY123456-2'),
  Policy(riskName: 'Farnesbarnes', policyNumber: 'PY123647-1')
];

So basically ShowPolicy returns a text widget. It has a constructor that accepts a Policy object. The Policy class contains 2 member variables, riskName and policyNumber.

My question(s) are :-

What owns the allPolicies variable declared at the end? Why is it allowed here and why doesn't it have to be declared in a class?

But my main question is:- Why are the Policy class constructor variables wrapped in curly-brackets {} ? If I remove them, the Policy class still compiles, but now the allPolicies variable doesn't (error message is '2 required arguments expected but 0 found')

Simon Hutton
  • 1,677
  • 3
  • 22
  • 35

2 Answers2

5

What owns the allPolicies variable declared at the end? Why is it allowed here and why doesn't it have to be declared in a class?

In Dart, it is allowed to have top-level variable and top-level functions. It is not required to have them in a class.

Why are the Policy class constructor variables wrapped in curly-brackets {} ?

Parameters inside curly-brackets are optional named parameters.

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • 1
    Thanks for your response. I'd read the 'A Tour Of The Dart Language' and found it a really helpful reference. I must have just plain missed the Optional Names Parameters {} bit – Simon Hutton Feb 20 '19 at 14:25
1

What owns the allPolicies variable declared at the end? Why is it allowed here and why doesn't it have to be declared in a class

  • allPolicies is owned by the dart file. In this case allPolicies is a global variable that can be accessed in any point of this file and in this case can be access too by the files that includes the file where allPolicies was defined. This because allPolicies was defined as public variable.

But my main question is:- Why are the Policy class constructor variables wrapped in curly-brackets {} ? If I remove them, the Policy class still compiles, but now the allPolicies variable doesn't (error message is '2 required arguments expected but 0 found')

  • Named parameters. When you put parameter like this xClass( {String myParamName } ) when you will create a instance of xClass you will pass the parameter values by his names like a map //key value
    xClass(myParamName: "myParamValue" )

This is cool because avoid parameters order and missmatch. More details here

error message is '2 required arguments expected but 0 found' - In this case is because the named parameters are required. There is two ways for named parameters be required. First way as you already see is using final class member as constructor named parameter like:

class Policy {
  final String riskName; //final class member
  final String policyNumber; // final class member
  Policy( { this.riskName, this.policyNumber } ); // class members as named parameters

}

Other way is using @required anotation. This will looks like this:

MyConstructor( {@required String param1, @required int param2} ){ ... }
Marcos Boaventura
  • 4,641
  • 1
  • 20
  • 27
  • 1
    Thanks for your response. I'd read the 'A Tour Of The Dart Language' and found it a really helpful reference. I must have just plain missed the Optional Names Parameters {} bit – Simon Hutton Feb 20 '19 at 14:26