I have this listview builder in flutter where I need to get the data for it from cloudcode but I don’t know whether to pass is through a FutureBuilder or using an async class. Their is also an issue with having FutureBuilder inside of the Expanded widget and then nesting the ListView inside. This is on parse_server_sdk_flutter: ^3.0.0. I’m trying to do this the right way instead of using some weird workaround and I couldn’t find a definitive answer on the parse flutter github.
Here is the cloudcode portion
Parse.Cloud.define("getTix", async (request) => {
const query = new Parse.Query("Ticket");
query.equalTo("home", request.params.home);
const results = await query.find({ useMasterKey: true });
let ticket = [];
for (let i = 0; i < results.length; i++) {
ticket = results[i];
}
return ticket;
});
Here is my flutter file for this portion
class WalletPage extends StatefulWidget {
@override
_WalletPageState createState() => _WalletPageState();
}
class _WalletPageState extends State<WalletPage> {
bool closeTopContainer = false;
double topContainer = 0;
List<Widget> itemsData = [];
List<dynamic> testList = [];
void getPostsData() {
List<dynamic> responseList = seatData;
List tixList = await getTickets();
List<Widget> listItems = [];
responseList.forEach((post) {
listItems.add(
GestureDetector(
dragStartBehavior: DragStartBehavior.start,
onTap: () => _openPage((_) => TicketPage()),
onHorizontalDragStart: (DragStartDetails details) =>
_openPage((_) => SellingPage()),
child: Container(
height: 150,
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black.withAlpha(100), blurRadius: 10.0),
]),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
post["event"],
style: const TextStyle(
fontSize: 24, fontWeight: FontWeight.bold),
),
Text(
post["section"],
style:
const TextStyle(fontSize: 17, color: Colors.grey),
),
Text(
post["row"],
style:
const TextStyle(fontSize: 17, color: Colors.grey),
),
Text(
post["seat"],
style:
const TextStyle(fontSize: 17, color: Colors.grey),
),
],
),
),
Image.asset(
"assets/images/${post["image"]}",
height: 100,
width: 100,
)
],
),
),
),
),
);
});
setState(() {
itemsData = listItems;
});
}
Future<List<Ticket>> getTickets() async {
final ParseCloudFunction function = ParseCloudFunction('getTix');
final Map<String, dynamic> params = <String, dynamic>{'home': 'Cubs'};
final response = await function.execute(parameters: params);
List<Ticket> ticketList =
response.results!.map((request) => (request as Ticket)).toList();
return ticketList;
}
@override
void initState() {
super.initState();
getPostsData();
getTickets();
}
_openPage(WidgetBuilder pageToDisplayBuilder) {
Navigator.push(
context,
platformPageRoute(
context: context,
builder: pageToDisplayBuilder,
));
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
//final double categoryHeight = size.height * 0.30;
return SafeArea(
child: PlatformScaffold(
backgroundColor: Colors.grey[900],
body: Container(
height: size.height,
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text(
" Events",
style: TextStyle(
color: Colors.yellow,
fontSize: 48,
fontWeight: FontWeight.w700,
fontFamily: "SF UI Display",
),
textAlign: TextAlign.start,
),
],
),
const SizedBox(
height: 10,
),
Expanded(
child:
FutureBuilder(
builder: (context, ticketSnap) {
if (ticketSnap.connectionState == ConnectionState.none && ticketSnap.hasData) {
return Container();
}
return ListView.builder(
itemCount: 10,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
double scale = 1.0;
return Opacity(
opacity: scale,
child: Align(
heightFactor: 0.7,
alignment: Alignment.topCenter,
child: ticketSnap.data![index]
),
);
},
);
},
future: getTickets(),
),
),
],
),
),
),
);
}
}