I'm getting a teacher model from another page using constructor. Also I'm passing that model to custom made screens.. But on first loading all screen are getting null even I am building the widgets after when fetching is completed.
Error shows on ** screens,**
class TeacherMainScreen extends StatefulWidget {
TeacherMainScreen({super.key, required this.teacherModel});
TeacherModel teacherModel;
@override
State<TeacherMainScreen> createState() => _TeacherMainScreenState();
}
class _TeacherMainScreenState extends State<TeacherMainScreen> {
TeacherModel? teacherModel;
late bool isLoading;
// late bool isConnected;
@override
void initState() {
isLoading = true;
getData();
super.initState();
}
void getData() async {
teacherModel = await Database().teacherAllData().whenComplete(() {
setState(() {
isLoading = false;
});
});
}
int index = 0;
@override
Widget build(BuildContext context) {
if (teacherModel == null) {
debugPrint('teacher model is not available for null');
} else {
debugPrint('teacher model is gotted');
}
var screen = [
TeacherHomeScreen(model: teacherModel),
TeacherSessionScreen(teacherrModel: teacherModel),
PersonalAcountScreen(teacherModel: teacherModel),
];
if (isLoading) {
return Scaffold(body: customLoading());
}
else {
return Scaffold(
backgroundColor: Colors.white.withOpacity(0.1),
body: screen[index],
bottomNavigationBar: NavigationBarTheme(
data: NavigationBarThemeData(
indicatorColor: AppColors.PRIMARY_COLOR_20,
surfaceTintColor: Colors.transparent,
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
labelTextStyle: MaterialStateProperty.all(
TextStyle(fontSize: 14, fontFamily: 'Inter'))),
child: NavigationBar(
backgroundColor: Colors.white,
selectedIndex: index,
onDestinationSelected: (value) {
setState(() {
index = value;
});
},
destinations: [
NavigationDestination(icon: AppIcon.NEAR_ME, label: 'Explore'),
NavigationDestination(
icon: AppIcon.CALENDER_MONTH, label: 'Sessions'),
NavigationDestination(icon: AppIcon.PERSON, label: 'Account')
],
),
),
);
}
}
I am passing that teacher model to all the screen like,, But on first loading it shows null.. How can I solve this?
You have used async in your method and this means that your data is loaded with a delay I suggest that instead of putting getData inside the init method, call it inside the FutureBuilder so that you have control over receiving the information from snapShot and when the information is received, it will perform the initialization operation for the pages.
first of all change getData return type to Future<bool>
and
check inside getData method if data feach then return true otherways return false
then put all scafold body inside FutureBuilder and as future pas getData method and inside builder check if snapshot true that means data feached so you can init screen data otherways show custom progresbar