I have a FutureBuilder
and inside is a PageView.builder
. The future
is calling an api and a list of flags is being fetched from the server. The flags are based on '0'
& '1'
like this ['1','1','0','0']
. What I want to do is to skip the pages from start that have flag ‘1’. In this case, page first should be the first '0'
at index 2
. However, all pages should be built in case user want to see the previous pages as well.
Here is my code:
import 'package:flutter/material.dart';
class ScreenForBuilder extends StatefulWidget {
const ScreenForBuilder({Key? key}) : super(key: key);
@override
State<ScreenForBuilder> createState() => _ScreenForBuilderState();
}
class _ScreenForBuilderState extends State<ScreenForBuilder> {
List isCompleted = [];
apiCallFunction() async {
isCompleted = ['1','1','0','0'];
return isCompleted;
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: FutureBuilder(
future: apiCallFunction(),
builder: (context, snapshot){
return PageView.builder(
itemCount: 4,
itemBuilder: (context, index){
return Center(
child: Text('${index+1}', style: const TextStyle(fontSize: 50),),
);
}
);
}
),
),
);
}
}
2
Answers
The code @Hydra provided worked great for me but here is an alternative method. I set the initial page with
indexOf
a flag. Here's my code that also worked.you can set the
initialPage
in thePageController