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')