nixrajput / flutter_carousel_widget

A customizable carousel slider widget in Flutter which supports infinite scrolling, auto scrolling, custom child widget, custom animations and built-in indicators.
https://pub.dev/packages/flutter_carousel_widget
MIT License
27 stars 16 forks source link

Unhandled Exception: Null check operator used on a null value #6

Closed bradintheusa closed 1 year ago

bradintheusa commented 1 year ago

I get an error because state is null in the most recent version.

Any ideas what I'm missing?

I'll post more code if needed.

flutter_carousel_controller.dart

  Future<void> nextPage(
      {Duration? duration = const Duration(milliseconds: 300),
      Curve? curve = Curves.linear}) async {
    final isNeedResetTimer = _state!.options.pauseAutoPlayOnManualNavigate;
    if (isNeedResetTimer) {
      _state!.onResetTimer();
    }
    _setModeController();
    await _state!.pageController!.nextPage(duration: duration!, curve: curve!);
    if (isNeedResetTimer) {
      _state!.onResumeTimer();
    }
  }
nixrajput commented 1 year ago

Hi @bradintheusa,

Please share the detailed code of widget integration and how are you implementing the logic.

I would be happy to help you.

Thanks. @nixrajput

bradintheusa commented 1 year ago

I updated the demo

https://github.com/bradintheusa/flutter_carousel_widget

I'm guessing it's the future builder or just my bad code.

nixrajput commented 1 year ago

Yeah, this is not a widget issue.

Can you share your output screenshot or error log??

bradintheusa commented 1 year ago

Here, is this what you need.

image

syeduzairdev commented 1 year ago

its happen because your variable _state is null kindly make the check condition for this

bradintheusa commented 1 year ago

Agreed, but that's set and managed by the component not the application. Hence the question.

nixrajput commented 1 year ago

Hi @bradintheusa

I have gone through your code and found that you haven't added the CarouselController in FlutterCarousel.

That is why this issue Null State is coming.

I have updated your code now.

For your reference, I am adding the updated code here.

Thanks.

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

class NextPrevState extends StatefulWidget {
  const NextPrevState({Key? key}) : super(key: key);

  @override
  State<NextPrevState> createState() => NextPrevStateState();
}

class NextPrevStateState extends State<NextPrevState> {
  Future<String>? _value;
  final CarouselController _controller = CarouselController();
  int _currIndex = 0;

  Future<String> getValue() async {
    await Future.delayed(const Duration(seconds: 3));
    return 'Flutter Devs';
  }

  @override
  void initState() {
    super.initState();
    setState(() {
      _value = getValue();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Image Slider Demo')),
      body: Column(
        children: [
          FutureBuilder<String>(
            future: _value,
            builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
              if (snapshot.hasData) {
                return Column(
                  children: [
                    Text('Current Index: $_currIndex'),
                    const SizedBox(height: 10.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(left: 10),
                          child: GestureDetector(
                            onTap: _controller.previousPage,
                            child: const Icon(
                              Icons.chevron_left,
                              size: 50,
                              color: Colors.black,
                            ),
                          ),
                        ),
                        Expanded(
                            child: Align(
                                alignment: Alignment.center,
                                child: Text(
                                  snapshot.data!,
                                ))),
                        Padding(
                          padding: const EdgeInsets.only(left: 10),
                          child: GestureDetector(
                            onTap: _controller.nextPage,
                            child: const Icon(
                              Icons.chevron_right,
                              size: 50,
                              color: Colors.black,
                            ),
                          ),
                        ),
                      ],
                    ),
                    FlutterCarousel(
                      options: CarouselOptions(
                          showIndicator: true,
                          autoPlay: false,
                          slideIndicator: const CircularSlideIndicator(),
                          onPageChanged: (index, reason) {
                            setState(() {
                              _currIndex = index;
                            });
                          }),
                      items: ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
                          .map((i) {
                        return Builder(
                          builder: (BuildContext context) {
                            return Text(i);
                          },
                        );
                      }).toList(),
                      carouselController: _controller,
                    ),
                  ],
                );
              }
              return const Text('Loading');
            },
          )
        ],
      ),
    );
  }
}
nixrajput commented 1 year ago

I hope your issue has been resolved, so I am closing this issue for now.

You can reopen it if you still have any issues.

bradintheusa commented 1 year ago

Much appreciated.