I am getting a List of data from third-party API on one page and using the same data on another page but I don't know how to make the list iterable to access indexes in the Widget Build() method.
On this page, I am getting List.
class DetailsPage extends StatefulWidget {
List<MarkersOnMap> propertydetails;
DetailsPage({Key key, this.propertydetails})
: super(
key: key,
);
@override
_DetailsPageState createState() => _DetailsPageState();
}
This is a Build() method and I want to access index as variable on each call, currently I'm just using index [0]
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: Text(widget.propertydetails[0].ePropertiesCardsList.name,
style: appBarTextStyle),
),
bottomNavigationBar: Material(
child: Container(
child: Row(
children: <Widget>[
Row(
children: [
Text(
widget.propertydetails[0].ePropertiesCardsList.price
.toString(),
style: blackBigBoldTextStyle,
),
Text(
widget.propertydetails[0].ePropertiesCardsList.status,
style: blackSmallTextStyle,
),
],
),
child: Container(
child: Text(
'Contact'),
),
),
],
),
),
),
body: ListView(
children: [
Hero(
tag: widget.propertydetails[0].ePropertiesCardsList.name,
child: slider(),
),
titleRating(),
],
),
);
}
Sending data from the page like following
_propertyFinalList(index) {
return Container(
child: Center(
child: InkWell(
onTap: () async {
var get = await future;
if (get.isNotEmpty) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(
propertydetails: get,
)),
);
}
},
CodePudding user response:
Not 100 percent sure I got the question right but to keep it simple you could use a global static class to store the data and access it from different parts of your application.
class GlobalState {
static List<MarkersOnMap> propertydetails = [];
static int selectedIndex = 0;
}
Then you would be able to write and read this data from different Pages. Keep in mind that your view will not rerender if you change data in this GlobalState, for that you could take a look at: https://pub.dev/packages/get
CodePudding user response:
I just solved the problem by changing the following code from this
MaterialPageRoute(
builder: (context) => DetailsPage(
propertydetails: get,
)),
);
to this code. The full code is available above in the question.
MaterialPageRoute(
builder: (context) => DetailsPage(
propertydetails: [get[index]],
)),
);
