Skip to content

Null check operator used on a null value with PageView's controller #85867

Closed

Description

@pedromassango

Facing this for few days now on the latest stable. Haven't found any existing issue so...

Steps to Reproduce

  1. Run the code below.

Expected results: No crash

Actual results: Null check operator used on a null value

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: Page()));

class Page extends StatefulWidget {
  @override
  _BankAppState createState() => _BankAppState();
}

class _BankAppState extends State<Page> {
  late PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
        itemCount: 4,
        controller: _pageController,
        itemBuilder: (context, index) {
          // Calling this cause the issue.
          debugPrint(_pageController.page.toString());
          return Container(color: Colors.primaries[index]);
        },
      ),
    );
  }
}
Logs
Performing hot restart...
Syncing files to device iPhone 12 Pro Max...
Restarted application in 306ms.

======== Exception caught by widgets library =======================================================
The following _CastError was thrown building:
Null check operator used on a null value

When the exception was thrown, this was the stack: 
#0      ScrollPosition.minScrollExtent (package:flutter/src/widgets/scroll_position.dart:136:49)
#1      _PagePosition.page (package:flutter/src/widgets/page_view.dart:388:22)
#2      PageController.page (package:flutter/src/widgets/page_view.dart:181:21)
#3      _BankAppState.build.<anonymous closure> (package:flutter_challenges/banking_app.dart:69:38)
#4      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:455:22)
...
====================================================================================================
flutter: 0.0
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.4 20F71 darwin-arm, locale en-AO)
    • Flutter version 2.2.3 at /Users/pedromassango/dev/sdk/flutter_stable
    • Framework revision f4abaa0735 (3 days ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4

Activity

  1. darshankawar commented on Jul 5, 2021

    @darshankawar
    Member

    @pedromassango Hi,
    Any reason you are using debugPrint(_pageController.page.toString()); instead of debugPrint(_pageController.toString()); ?
    Using the latter, it works and prints properly as below:

    flutter: PageController#88bfb(one client, offset 0.0)
    flutter: PageController#88bfb(one client, offset 32.0)
    flutter: PageController#88bfb(one client, offset 445.8)
    flutter: PageController#88bfb(one client, offset 857.2)
    
  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jul 5, 2021
  3. pedromassango commented on Jul 5, 2021

    @pedromassango
    MemberAuthor

    Any reason you are using debugPrint(_pageController.page.toString()); instead of debugPrint(_pageController.toString()); ?

    No really. However, the point it that this code throws an exception.

    You can replace that with: _pageController.page!.toDouble() which is what I was trying to do.

    On a side note: this code still crashes even if you define the initialPage in the controller.

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jul 5, 2021
  5. darshankawar commented on Jul 6, 2021

    @darshankawar
    Member

    Thanks for the details.

    
    Performing hot restart...
    Syncing files to device iPhone 12 Pro Max...
    Restarted application in 418ms.
    
    ======== Exception caught by widgets library =======================================================
    The following _CastError was thrown building:
    Null check operator used on a null value
    
    When the exception was thrown, this was the stack: 
    #0      ScrollPosition.minScrollExtent (package:flutter/src/widgets/scroll_position.dart:136:49)
    #1      _PagePosition.page (package:flutter/src/widgets/page_view.dart:388:22)
    #2      PageController.page (package:flutter/src/widgets/page_view.dart:181:21)
    #3      _BankAppState.build.<anonymous closure> (package:macbug/main.dart:21:38)
    #4      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:455:22)
    ...
    ====================================================================================================
    flutter: 0.0
    
    flutter doctor -v
    [✓] Flutter (Channel stable, 2.2.3, on Mac OS X 10.15.4 19E2269 darwin-x64,
        locale en-GB)
        • Flutter version 2.2.3 at /Users/dhs/documents/fluttersdk/flutter
        • Framework revision f4abaa0735 (4 days ago), 2021-07-01 12:46:11 -0700
        • Engine revision 241c87ad80
        • Dart version 2.13.4
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.10.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.57.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios
          • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                               •
          web-javascript • Google Chrome 91.0.4472.114
    
    • No issues found!
    
    
    
    
    
    
  6. added
    a: null-safetySupport for Dart's null safety feature
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    a: error messageError messages from the Flutter framework
    and removed
    in triagePresently being triaged by the triage team
    on Jul 6, 2021
  7. ominibyte commented on Jul 10, 2021

    @ominibyte

    This error is not just particular to PageView. I'm using ListWheelScrollView.useDelegate with FixedExtentScrollPhysics and FixedExtentScrollController and I have the same issue.

    Seems like a few null checks needs to be added to package:flutter/src/widgets/scroll_position.dart.

  8. Levi-Lesches commented on Aug 13, 2021

    @Levi-Lesches
    Contributor

    Related to #60578 (not quite a duplicate since that was initially about something else, but now they're the same).

  9. DieGlueckswurst commented on Aug 13, 2021

    @DieGlueckswurst

    really annyoing issue. Wish Flutter would update this! Is there ay workaround for this at the moment? @Levi-Lesches

  10. Levi-Lesches commented on Aug 13, 2021

    @Levi-Lesches
  11. DieGlueckswurst commented on Aug 14, 2021

    @DieGlueckswurst
  12. Levi-Lesches commented on Aug 15, 2021

    @Levi-Lesches
  13. pedromassango commented on Sep 6, 2021

    @pedromassango
    MemberAuthor

    Finally found a workaround for this bug: Make sure you check for the content dimensions before trying to access a content-related property.

        if (!_scrollController.position.hasContentDimensions) {
          return;
        }
        // It is now safe to use anything related to position.
        final maxScrollExtent = _scrollController.position.maxScrollExtent;
    
  14. markusaksli-nc commented on Nov 25, 2021

    @markusaksli-nc
    Contributor

    This now throws an assertion that explains this so closing this as fixed.

    Page value is only available after content dimensions are established.

  15. github-actions commented on Dec 9, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  16. locked as resolved and limited conversation to collaborators on Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: error messageError messages from the Flutter frameworka: null-safetySupport for Dart's null safety featurefound in release: 2.3Found to occur in 2.3frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions