Fred_Wolfe
Fred_Wolfe

Reputation: 822

error: The argument type 'Null Function(int)' can't be assigned to the parameter type 'dynamic Function(int, CarouselPageChangedReason)'

I am using the carousel Slide plugins too create a carousel slide image, but when I initialize a variable of "int" _current = 0; and I create a set state to assign the variable to it it gives me an error. "Null function(int)' can't be assigned to the parameter type 'dynamic Function(int, CarouselPageChangedReason)'"

class HomePage extends StatefulWidget { static String page = 'home_Page';

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _current = 0;

  List imgList = [
    'assets/images/deal1.jpg',
    'assets/images/deal4.jpg',
    'assets/images/deal2.jpg',
    'assets/images/deal3.png',
  ];

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Scaffold(
      appBar: buildAppBar,
      drawer: Drawer(
        child: DrawerPage(),
      ),
      body: Container(
        margin: EdgeInsets.only(left: 14),
        child: Container(
          height: getProportionateScreenHeight(820),
          child: ListView(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  welcomeHeader(),
                  SizedBox(height: getProportionateScreenHeight(10)),
                  SearchField(),
                  SizedBox(height: getProportionateScreenHeight(20)),
                  categorySectionTitle(),
                  SizedBox(height: getProportionateScreenWidth(5)),
                  categoryListTiles(),
                  SizedBox(height: getProportionateScreenHeight(20)),
                  SizedBox(height: getProportionateScreenHeight(10)),
                  popularFoodSectionTiles(),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      CarouselSlider(
                        options: CarouselOptions(
                          height: 100,
                          initialPage: 0,
                          onPageChanged: (index) {
                            setState(() {
                              _current = index;
                            });
                          },
                        ),enter code here
                        items: [],
                      ),
                    ],
                  ),
                  SizedBox(height: getProportionateScreenHeight(20)),
                  restaurantSplendorTiles(),
                  SizedBox(height: getProportionateScreenWidth(5)),
                  RestaurantCat(),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This is the error line of code is:

onPageChanged: (index) {
                            setState(() {
                              _current = index;
                            });
                          }

,

help me please... :(

Upvotes: 0

Views: 2597

Answers (2)

Fred_Wolfe
Fred_Wolfe

Reputation: 822

I found the Answer, Instead of ;

onPageChanged: (index) {
                            setState(() {
                              _current = index;
                            });
                          }

it should be ;

onPageChanged: (index, reason) {
                            setState(() {
                              _current = index;
                            });
                          }

Upvotes: 1

Ben Baldwin
Ben Baldwin

Reputation: 497

it looks like you need to accept another argument into the onPageChanged function. something like this should work...

onPageChanged: (index, _) {
  setState(() {
    _current = index;
  });
}

Upvotes: 1

Related Questions