I made a json
request on my main page. It returns a list of values. I want to send these values to one of my tabBar
pages. I have little idea how to go about it guys.
TabOptions.dart
class Choice {
final String title;
final IconData icon;
const Choice({this.title, this.icon});
}
const List<Choice> choices = <Choice>[
Choice(title: 'Conversation', icon: Icons.comment),
Choice(title: 'Schedule', icon: Icons.schedule),
Choice(title: 'Requests', icon: Icons.subdirectory_arrow_left),];
class ChoicePage extends StatelessWidget {
const ChoicePage({Key key, this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
final TextStyle textStyle = Theme.of(context).textTheme.display1;
return Card(
color: Colors.white,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
choice.icon,
size: 150.0,
color: textStyle.color,
),
Text(
choice.title,
style: textStyle,
),
],
),
),
);
}
}
MainScreen
class DashBoardPage extends StatefulWidget {
@override
_DashBoardPageState createState() => _DashBoardPageState();
}
class _DashBoardPageState extends State<DashBoardPage> {
List<MentorIndex> mentorIndexes = [];
SharedPreferences sharedPreferences;
Iterable newList;
Widget callPage(int currentIndex) {
switch (currentIndex) {
case 0:
showTabs = true;
_appBarText = "Welcome, " + _firstName;
return TabBarView(
children: [new HomePage(), new SchedulePage(), RequestsPage()]);
break;
}
@override
void initState() {
super.initState();
fetchIndex();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '',
debugShowCheckedModeBanner: false,
home: DefaultTabController(
length: choices.length,
child: Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFFFFFFFF),
title: Text(
_appBarText,
bottom: showTabs
? TabBar(
isScrollable: true,
tabs: choices.map<Widget>((Choice choice) {
return Tab(
text: choice.title,
icon: Icon(choice.icon),
);
}).toList(),
labelColor: Color(0xFF1C2447),
)
: null,
actions: <Widget>[
],
), //AppBar
},
items: [
],
),
),
),
);
}
Future fetchIndex() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var uri = NetworkUtils.host + AuthUtils.endPointIndex;
try {
final response = await http.get(
uri,
headers: {'Accept': 'application/json', 'Content-Type': 'application/json','Authorization': 'Bearer ' + sharedPreferences.get("token"), },
);
final responseJson = json.decode(response.body);
for (var u in responseJson["data"]) {
MentorIndex user = MentorIndex(
u["id"],
u["mentor_id"],
u["mentee_id"],
u["status"],
u["session_count"],
u["current_job"],
u["email"],
u["phone_call"],
u["video_call"],
u["face_to_face"],
u["created_at"],
u["updated_at"]);
mentorIndexes.add(user);
newList = mentorIndexes.map((MentorIndex) => MentorIndex.mentee_id);
//here i want to make this newList data available to my tabBar pages
}
return responseJson;
} catch (exception) {
print(exception);
}
}
}
It looks like you aren't using mentorIndexes on the mainscreen so why not make the http request in the widget where you are using the data?
Also, if you indend on using the mentorIndexes in multiple widgets, it's recommended that use a state management solution. The flutter teams recommends the Provider Package but you can read their official guide to see the options available to you. They also have a great example using provider.
Add new TabBarView
include pages in Scaffold
body
.
If only one page needs the values then call the values in page :
Scaffold(
body: TabBarView(
children: [HomePage(), SchedulePage(), RequestsPage()],
)
)
Else use FutureBuilder
:
body: FutureBuilder(
future:fetchIndex(),
builder: (context, value) => TabBarView(
children: [HomePage(newList), SchedulePage(newList), RequestsPage(newList)],
),
User contributions licensed under CC BY-SA 3.0