Home > other >  Pageview.Builder inside Futurebuilder is throwing errors
Pageview.Builder inside Futurebuilder is throwing errors

Time:12-29

I'm relatively new to Flutter So I have the following code, I've been reading futurebuilder and it seems to be the most appropriate builder for my situation.

class CardCarousel extends StatefulWidget {
  const CardCarousel({super.key});

  @override
  State<CardCarousel> createState() => _CardCarouselState();
}

class _CardCarouselState extends State<CardCarousel> {
  List<String> imagePaths = [];

  @override
  void initState() {
    super.initState();
    _initImages();
  }

  Future<List<String>> _initImages() async {
    final manifestContent = await rootBundle.loadString('AssetManifest.json');
    final Map<String, dynamic> manifestMap = json.decode(manifestContent);
    List<String> imagePaths = manifestMap.keys
        .where((String key) => key.contains('assets/images/png/'))
        .where((String key) => key.contains('.png'))
        .toList();

    return imagePaths;
  }

  @override
  Widget build(BuildContext context) {
    final PageController controller = PageController();
    return FutureBuilder(
        future: _initImages(),
        builder: (context, AsyncSnapshot<List<String>> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.done:
              if (snapshot.data == null) {
                return Text('NO IMAGES');
              } else {
                setState(() {
                  imagePaths = snapshot.data!;
                });
              }
              break;
            case ConnectionState.none:
              print('NONE');
              break;
            case ConnectionState.waiting:
              print('WAITING');
              break;
            case ConnectionState.active:
              print('active');
              break;
          }

          return PageView.builder(
              controller: controller,
              itemCount: imagePaths.length,
              itemBuilder: (_, int index) {
                return CustomCard(imageURL: imagePaths[index]);
              });
        });
  }
}

it throws the following error:

This CardCarousel widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.

Is this an instance where a particular entry is null? or should I add more conditional renders?

CodePudding user response:

You shouldn't call setState there, so replace

          if (snapshot.data == null) {
            return Text('NO IMAGES');
          } else {
            setState(() {
              imagePaths = snapshot.data!;
            });
          }

with

          if (snapshot.data == null) {
            return Text('NO IMAGES');
          } else {
            imagePaths = snapshot.data!;
          }
  • Related