Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bradintheusa opened this issue Aug 30, 2022 · 9 comments
Closed

Comments

@bradintheusa
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

Yeah, this is not a widget issue.

Can you share your output screenshot or error log??

@bradintheusa
Copy link
Author

Here, is this what you need.

image

@syeduzairdev
Copy link

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

@bradintheusa
Copy link
Author

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

@nixrajput
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants