Hello everyone I am using the Get.to(Page()) and when I navigate to the page I get the following error and I don't understand why:
Error:
======== Exception caught by widgets library
=======================================================
The following TypeErrorImpl was thrown building personalDocs$(dirty, state: _personalDocsState#333f6):
Expected a value of type 'String', but got one of type 'Null'
The relevant error-causing widget was:
personalDocs$ personalDocs:file:///home/ivanoiupaul/StudioProjects/hipt/lib/DashBoardControl/userData.dart:152:32
When the exception was thrown, this was the stack:
I will attach the userData.dart so you can see were it gives me the error:
TextButton(
onPressed: () async {
userDataForPersonalData = await getData(userDataID);
Get.to(personalDocs());
},
child: ContainerCustom(
MediaQuery.of(context).size.width / 2,
100,
25,
"PERSONAL DOCUMENTS",
FontWeight.w600),
),
Now I will attach the page where it goes to:
class _personalDocsState extends State<personalDocs> {
bool? enabledd = false;
Map<String, dynamic>? userDataToSendToDashBoard3 = {};
@override
void initState() {
// TODO: implement initState
super.initState();
() async {
userDataToSendToDashBoard3 = await getData(userDataID);
setState(() {});
}();
}
@override
Can anyone indicate me what I am doing wrong ? Thank you
CodePudding user response:
since you are using an async method inside your initState() method the variable userDataToSendToDashBoard3
is being initialized a bit later than it is being used while building the widget tree the first time and hence it is null
there. So you need to have any loading kind of thing while the async method finishes, and after that just use the setState
to rebuild the widget with the condition for loading false and userDataToSendToDashBoard3
with a non-null value.
CodePudding user response:
So for anyone having this problem, the problem is not in the navigation but in the page where you navigate to.
Basically, you have to check very carefully that all the data that you are fetching from the internet is there and all the fields that display the data are the right fields in order for them to work otherwise you will get the error that I get. Once I check all the fields I saw that two of them were null because I misspelled some words and the IDE did not underline them.
So be careful and check the fields.