skip to Main Content

I’m Trying To Show A Slider Using The Following Package fan_carousel_image_slider 0.1.2

Full Error Text:

_AssertionError (‘package:fan_carousel_image_slider/src/image_slider.dart’: Failed
assertion: line 49 pos 16: ‘initalPageIndex <= (imagesLink.length – 1)
&& initalPageIndex >= 0’: is not true.)

I Tried To Change Attributes And Images Sizes But Without Any Result.

import 'package:fan_carousel_image_slider/fan_carousel_image_slider.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const Outlet());
}

class Outlet extends StatelessWidget {
  const Outlet({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Outlet',
      theme: ThemeData(
        primarySwatch: Colors.red,
        primaryColor: Colors.red[800],
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});
  static List<String> sampleImages = [
    "assets/images/Sample (1).jpg"
        "assets/images/Sample (2).jpg"
        "assets/images/Sample (3).jpg"
        "assets/images/Sample (4).jpg"
  ];

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            FanCarouselImageSlider(
              imagesLink: MyHomePage.sampleImages,
              isAssets: true,
              autoPlay: true,
            ),  
   ],
 ),

2

Answers


  1. Chosen as BEST ANSWER
    import 'package:fan_carousel_image_slider/fan_carousel_image_slider.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const Outlet());
    }
    
    class Outlet extends StatelessWidget {
      const Outlet({super.key});
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Outlet',
          theme: ThemeData(
            primarySwatch: Colors.red,
            primaryColor: Colors.red[800],
          ),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key});
      static List<String> sampleImages = [
        "assets/images/Sample (1).jpg"
            "assets/images/Sample (2).jpg"
            "assets/images/Sample (3).jpg"
            "assets/images/Sample (4).jpg"
      ];
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    int get initialPage => 1;
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: Column(
              children: <Widget>[
    *SizedBox(
                    height: 190,
                    width: 400,
                    child: FanCarouselImageSlider(
                      imagesLink: MyHomePage.sampleImages,
                      isAssets: true,
                      autoPlay: true,
                      expandedImageFitMode: BoxFit.fill,
                      expandedCloseBtn: Icon(
                        Icons.arrow_circle_left_rounded,
                        color: Colors.red.shade500,
                        size: 30,
                      ),
                      indicatorActiveColor: Colors.red,
                      autoPlayInterval: const Duration(milliseconds: 3000),
                      sliderHeight:
                          170, //MUST Be Less Than The Sized Box height By 20.0 p
                      sliderWidth: 400,
                    ),
                  ),*
       ],
     ),
    

    -Solved By Creating Defined Size SizedBox Instead Of Just Being Part Of The Column -And Setting The Initial Page Index To Start From 1


  2. Remove Static keyword and move the list to below _MyHomPageState class:

    class TestPage extends StatefulWidget {
      const TestPage({Key? key}) : super(key: key);
    
      @override
      State<TestPage> createState() => _TestPageState();
    }
    
    class _TestPageState extends State<TestPage> {
    
      List<String> sampleImages = [
        "https://images.unsplash.com/photo-1557700836-25f2464e845d?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=627&q=80",
        "https://images.unsplash.com/photo-1669462277329-f32f928a4a79?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80",
        "https://images.unsplash.com/photo-1542840410-3092f99611a3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80",
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: FanCarouselImageSlider(
            imagesLink: sampleImages,
            isAssets: false,
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search