Reputation: 438
It's the first time a use Flutter (2.8.1) and I'having problems trying to undestrand what's going wrong.
I have a Stateful widget like this:
class SimpleWidget extends StatefulWidget {
final Type2 aValue;
const SimpleWidget({Key key, @required this.aValue}) : super(key: key);
@override
_SimpleWidgetState createState() => _SimpleWidgetState();
}
class _SimpleWidgetState extends State<SimpleWidget> {
Type1 color;
@override
void initState() {
super.initState();
color = widget.aValue; // <-- widget is null
}
...
}
that I call in this way:
List<Type1> something = await showDialog(
context: context,
builder: (context) {
print('currentElement.aValue: ${currentElement.aValue}'); // not null
return SimpleWidget(aValue: currentElement.aValue);
},
);
Why is widget.aValue == null
in initState()
? How can I solve it?
Upvotes: 0
Views: 121
Reputation: 1762
There are some error in your coding;
The first one is construction of SimpleWidget
const SimpleWidget({Key key, @required this.aValue}) : super(key: key);
when you call like SimpleWidget(aValue: currentElement.aValue);
It will should error like key can not be null
. You can use ?
to make it nullable. Also, @
should be remove, it is a syntax error
The correct one looks like
const SimpleWidget({Key? key, required this.aValue}) : super(key: key);
The second one is in SimpleWidgetState
You can change Type1 color;
to late Type1 color;
Or make it nullable.
For more details, you can check flutter codelabs
Upvotes: 1