Null value in Flutter widget inside initState method - flutter

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 {
const SimpleWidget({Key key, #required this.aValue}) : super(key: key);
final Type2 aValue;
#override
_SimpleWidgetState createState() => _SimpleWidgetState();
}
class _SimpleWidgetState extends State<SimpleWidget> {
Type1 from;
Type1 to;
#override
void initState() {
print('mounted: $mounted'); // true
print('widget.aValue: ${widget.aValue}'); // null <-- WHY IS THIS NULL?
super.initState();
from = ...;
to = ...;
}
...
}
that I call in this way:
List<Type1> breakTimes = await showDialog(
context: context,
builder: (context) {
print('currentElement.aValue: ${currentElement.aValue}'); // not null
return SimpleWidget(aValue: currentElement.aValue);
},
);
Why is widget.aValue == null in initInstance()? How can I solve it?

There is wrong in your code :
Chane this to this:
class SimpleWidget extends StatefulWidget {
final Type2 aValue; // initialize 1st
const SimpleWidget({Key key, #required this.aValue}) : super(key: key);
#override
_SimpleWidgetState createState() => _SimpleWidgetState();
}

Related

"'key' is required, but there's no corresponding argument" flutter error

How to solve this error?
The named parameter 'key' is required, but there's no corresponding argument. (Documentation) Try adding the required argument.
error
Future<void> onJoin() async {
// update input validation
setState(() {
_channelController.text.isEmpty
? _validateError = true
: _validateError = false;
});
if (_channelController.text.isNotEmpty) {
await _handleCameraAndMic(Permission.camera);
await _handleCameraAndMic(Permission.microphone);
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VideoCall(
channelName: _channelController.text,
role: _role,
),
),
);
}
}
class VideoCall
class VideoCall extends StatefulWidget {
final String channelName;
final ClientRole role;
const VideoCall({Key key, required this.channelName, required this.role})
: super(key: key);
#override
_VideoCallState createState() => _VideoCallState();
}
class _VideoCallState extends State<VideoCall> {
final _users = <int>[];
final _infoStrings = <String>[];
bool muted = false;
late RtcEngine _engine;
#override
void dispose() {
// clear users
_users.clear();
// destroy sdk
_engine.leaveChannel();
_engine.destroy();
super.dispose();
}
#override
void initState() {
super.initState();
// initialize agora sdk
initialize();
}
this is the videoCall class in there no any error shows.
when add "key" show this
When remove required property from key in video call class
show this error
In VideoCall class, key property set as a required, change it to optional:
class VideoCall extends StatefulWidget {
final String? channelName;
final ClientRole? role;
const VideoCall({Key? key, this.channelName, this.role})
: super(key: key);
#override
_VideoCallState createState() => _VideoCallState();
}

Widget is null inside initState

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?
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

The getter 'sundayIsSelected' was called on null. Receiver: null Tried calling: sundayIsSelected

here I created a model, and try to use the model in the page, but I get an error:
The getter 'sundayIsSelected' was called on null.
Receiver: null
Tried calling: sundayIsSelected
I set the sundayIsSelected in AlarmClock default value is false, why in the TimePickerPage there is still null? thanks!
Here is model code:
class AlarmClock {
bool sundayIsSelected;
//I also tried followings:
//bool sundayIsSelected = false;
AlarmClock({
this.sundayIsSelected = false,
});
}
here is my page code.
class TimePickerPage extends StatefulWidget {
final AlarmClock alarmClock;
const TimePickerPage({Key key, this.alarmClock}) : super(key: key);
#override
_TimePickerPageState createState() => _TimePickerPageState();
}
class _TimePickerPageState extends State<TimePickerPage> {
bool sundayIsSelected;
#override
void initState() {
sundayIsSelected = widget.alarmClock.sundayIsSelected;
}
Widget build(BuildContext context) {
Text(sundayIsSelected),
}
}
It is possible for alarmClock to be null, I will recommend adding required parameter on constructor and properly add others annotations.
class TimePickerPage extends StatefulWidget {
final AlarmClock alarmClock;
const TimePickerPage({Key? key, required this.alarmClock}) : super(key: key);
#override
_TimePickerPageState createState() => _TimePickerPageState();
}
class _TimePickerPageState extends State<TimePickerPage> {
late bool sundayIsSelected;
#override
void initState() {
sundayIsSelected = widget.alarmClock.sundayIsSelected;
super.initState();
}
#override
Widget build(BuildContext context) {
return Text(sundayIsSelected.toString());
}
}
And use it like
final alarmClock = AlarmClock(),
....
TimePickerPage(
alarmClock: alarmClock,
),
// or
TimePickerPage(
alarmClock: AlarmClock(),
)
...
Find more on docs.flutter.dev.

Pass to Widget a Function that returns a future

I want to pass to my Widget a function that returns a future:
class CircularButtonWithIcon extends StatefulWidget {
CircularButtonWithIcon(
{Key key,
#required this.onPress,
this.activeStatus})
: super(key: key);
final Function activeStatus;
class _CircularButtonWithIconState extends State<CircularButtonWithIcon> {
bool active;
#override
void initState() {
super.initState();
widget.activeStatus.then(...);
}
However Dart's class Function has no way to specify that the function's return type.
Is it possible to do such thing?
You can add the return type front of the Function
i.e.
class CircularButtonWithIcon extends StatefulWidget {
Future<void> Function() activeStatus;
CircularButtonWithIcon({Key key, #required this.onPress, this.activeStatus,}) : super(key: key);
}

How to access the variable from one class to other in flutter

I want to access the variable UniversityId in the class of _HomePageState
class showDetailOfUniversity extends StatelessWidget {
final String UniversityId;
showDetailOfUniversity({Key key, #required this.UniversityId}) : super(key:
key);
#override
Widget build(BuildContext context)
{
return (
HomePage()
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//
var temp2 = showDetailOfUniversity();
var temp = temp2.getUniversityId;
/// here i want to access the code but failed
}
The problem here is that you created another instance of showDetailOfUniversity that will have another values for its members. You did not initialize String UniversityId so its value is null untill you set it.
So when you called showDetailOfUniversity() in temp, the value of String UniversityId in this instance is null since there was no value given in this particular instance.
You can pass the String UniversityId in the constructor of the StatefulWidget like this:
class ShowDetailOfUniversity extends StatelessWidget {
final String universityId;
ShowDetailOfUniversity({Key key, #required this.universityId})
: super(key: key);
#override
Widget build(BuildContext context) {
return HomePage(universityId: universityId);
}
}
class HomePage extends StatefulWidget {
final String universityId;
const HomePage({Key key, this.universityId}) : super(key: key);
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var universityId;
#override
void initState() {
super.initState();
universityId = widget.universityId;
}
#override
Widget build(BuildContext context) {
// TODO: implement build
return Text(universityId);
}
}
Solution for your code:
var temp2 = showDetailOfUniversity(UniversityId: university_id);
var temp = temp2.UniversityId; //this will return university_id