'Pass Freezed Constructor Tear-Off to Generic Widget
What the title says. I have a freezed constructor tear-off that I'm trying to pass to a Widget and it's not returning null, and I'm trying to figure out what I'm doing wrong. Here is the freezed class:
@freezed
class PatientField with SetFieldOption, _$PatientField {
factory PatientField.firstName(String value) = PatientFirstName;
}
This is a simplified version of my initial Widget (which actually displays properly):
class SinglePatientView2 extends ConsumerWidget {
const SinglePatientView2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
print(PatientField.firstName('something'));
return Theme(
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: Expanded(
child: DefaultTabController(
length: 1,
child: Scaffold(
body: TabBarView(children: [
PersonTabContent<PatientField>(
PatientField.firstName,
)
])))));
}
}
Then Widget that's called above looks like (simplifed version anyway):
class PersonTabContent<T> extends StatelessWidget {
const PersonTabContent(
this.firstNameField, {
Key? key,
}) : super(key: key);
final T Function(String) firstNameField;
@override
Widget build(BuildContext context) {
print(firstNameField('something'));
return Padding(
padding: EdgeInsets.all(doubleBySize(context, 32)),
);
}
}
Looking at the output, the above prints:
PatientField.firstName(value: something)
null
I don't understand why I'm getting a null value in the second Widget's build method. Any insights as to what I'm doing wrong?
Solution 1:[1]
As usual, it was my fault. For anyone stumbling onto this, the problem was my version. Constructor tear-offs have only been recently implemented, and I was still specifying dart 2.15.0 in my pubspec.yaml file. For anyone else running into this issue, check your pubspec.yaml file and ensure the top looks like the following:
name: a_new_flutter_project
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.16.0 <3.0.0"
And that should be that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Grey |
