Codelessly / ResponsiveFramework

Easily make Flutter apps responsive. Automatically adapt UI to different screen sizes. Responsiveness made simple. Demo: https://gallery.codelessly.com/flutterwebsites/minimal/
https://codelessly.com
MIT License
1.28k stars 150 forks source link

Unexpected null value #154

Open tsafundzic opened 1 year ago

tsafundzic commented 1 year ago

Flutter Channel stable, 3.13.2 Responsive framework 1.1.1

On web and macOS I got error when resizing window:

> ======== Exception caught by widgets library =======================================================
> The following TypeErrorImpl was thrown building ScrollConfiguration(behavior: _WrappedScrollBehavior):
> Unexpected null value.
> 
> The relevant error-causing widget was: 
>   ResponsiveScaledBox ResponsiveScaledBox:file:///Users/xxx/StudioProjects/xxxx/lib/app.dart:28:22
> When the exception was thrown, this was the stack: 
> dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:49      throw_
> dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 855:63  nullCheck
> packages/xxxx/routing/router.g.dart 67:39                       $36SurveyIntroRouteExtension$124_fromState
> packages/go_router/src/route_data.dart 97:53                                      factoryImpl
> packages/go_router/src/route_data.dart 107:28                                     redirect
> packages/go_router/src/configuration.dart 464:34                                  [_getRouteLevelRedirect]
> packages/go_router/src/configuration.dart 460:9                                   processRouteRedirect
> packages/go_router/src/configuration.dart 480:14                                  [_getRouteLevelRedirect]
> packages/go_router/src/configuration.dart 422:13                                  processTopLevelRedirect
> packages/go_router/src/configuration.dart 438:16                                  processRedirect
> packages/go_router/src/configuration.dart 444:14                                  redirect
> packages/go_router/src/parser.dart 149:10                                         [_redirect]
> packages/go_router/src/parser.dart 74:12                                          parseRouteInformationWithDependencies
> packages/flutter/src/widgets/router.dart 743:12                                   [_processRouteInformation]
> packages/flutter/src/widgets/router.dart 622:7                                    restoreState
> packages/flutter/src/widgets/restoration.dart 923:5                               [_doRestore]
> packages/flutter/src/widgets/restoration.dart 909:7                               didChangeDependencies
> packages/flutter/src/widgets/router.dart 692:11                                   didChangeDependencies
> packages/flutter/src/widgets/framework.dart 5448:11                               [_firstBuild]
> packages/flutter/src/widgets/framework.dart 5273:5                                mount
> packages/flutter/src/widgets/framework.dart 4182:15                               inflateWidget
> packages/flutter/src/widgets/framework.dart 3701:20                               updateChild
> packages/flutter/src/widgets/framework.dart 5322:16                               performRebuild
> packages/flutter/src/widgets/framework.dart 5016:7                                rebuild
> packages/flutter/src/widgets/framework.dart 5628:5                                update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/framework.dart 5322:16                               performRebuild
> packages/flutter/src/widgets/framework.dart 5016:7                                rebuild
> packages/flutter/src/widgets/framework.dart 5373:5                                update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/framework.dart 6441:14                               update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/framework.dart 6441:14                               update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/framework.dart 5322:16                               performRebuild
> packages/flutter/src/widgets/framework.dart 5016:7                                rebuild
> packages/flutter/src/widgets/framework.dart 5373:5                                update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/framework.dart 6441:14                               update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/framework.dart 5322:16                               performRebuild
> packages/flutter/src/widgets/framework.dart 5016:7                                rebuild
> packages/flutter/src/widgets/framework.dart 5628:5                                update
> packages/flutter/src/widgets/framework.dart 3685:14                               updateChild
> packages/flutter/src/widgets/layout_builder.dart 135:18                           layoutCallback
> packages/flutter/src/widgets/framework.dart 2719:19                               buildScope
> packages/flutter/src/widgets/layout_builder.dart 153:5                            [_layout]
> packages/flutter/src/rendering/object.dart 2604:59                                <fn>
> packages/flutter/src/rendering/object.dart 1059:15                                [_enableMutationsToDirtySubtrees]
> packages/flutter/src/rendering/object.dart 2604:7                                 invokeLayoutCallback
> packages/flutter/src/widgets/layout_builder.dart 228:7                            rebuildIfNecessary
> packages/flutter/src/widgets/layout_builder.dart 313:5                            performLayout
> packages/flutter/src/rendering/object.dart 2493:7                                 layout
> packages/flutter/src/rendering/box.dart 2382:11                                   layout
> packages/flutter/src/rendering/proxy_box.dart 279:7                               performLayout
> packages/flutter/src/rendering/object.dart 2493:7                                 layout
> packages/flutter/src/rendering/box.dart 2382:11                                   layout
> packages/flutter/src/rendering/layout_helper.dart 52:10                           layoutChild
> packages/flutter/src/rendering/stack.dart 580:43                                  [_computeSize]
> packages/flutter/src/rendering/stack.dart 607:12                                  performLayout
> packages/flutter/src/rendering/object.dart 2332:7                                 [_layoutWithoutResize]
> packages/flutter/src/rendering/object.dart 1013:17                                flushLayout
> packages/flutter/src/rendering/binding.dart 494:19                                drawFrame
> packages/flutter/src/widgets/binding.dart 918:13                                  drawFrame
> packages/flutter/src/rendering/binding.dart 360:5                                 [_handlePersistentFrameCallback]
> packages/flutter/src/scheduler/binding.dart 1297:15                               [_invokeFrameCallback]
> packages/flutter/src/scheduler/binding.dart 1227:9                                handleDrawFrame
> packages/flutter/src/scheduler/binding.dart 1085:5                                [_handleDrawFrame]
> lib/_engine/engine/platform_dispatcher.dart 1304:13                               invoke
> lib/_engine/engine/platform_dispatcher.dart 278:5                                 invokeOnDrawFrame
> lib/_engine/engine/initialization.dart 185:45                                     <fn>
> dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 574:37  _checkAndCall
> dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 579:39  dcall
> ====================================================================================================

This is builder in MaterialApp, without it, app works without null exception:

 ResponsiveBreakpoints.builder(
        child: Builder(
          builder: (context) {
            return MaxWidthBox(
              maxWidth: 1200,
              background: Container(color: context.appColors.inputBorderUnfocused),
              child: ResponsiveScaledBox(
                width: ResponsiveValue<double>(
                  context,
                  conditionalValues: [
                    Condition.equals(name: MOBILE, value: 450),
                    Condition.between(start: 800, end: 1100, value: 800),
                    Condition.between(start: 1000, end: 1200, value: 1000),
                  ],
                ).value,
                child: BouncingScrollWrapper.builder(
                  context,
                  child!,
                  dragWithMouse: true,
                ),
              ),
            );
          },
        ),
        breakpoints: [
          const Breakpoint(start: 0, end: 450, name: MOBILE),
          const Breakpoint(start: 451, end: 800, name: TABLET),
          const Breakpoint(start: 801, end: 1920, name: DESKTOP),
          const Breakpoint(start: 1921, end: double.infinity, name: '4K'),

        ],
 ),
rayliverified commented 1 year ago

Thank you for reporting the issue and providing the sample code. Good issue!

rayliverified commented 1 year ago

Your responsive value doesn't cover all screen widths. So if the device is between 450 and 800, it returns a null value.

Either provide a default value ResponsiveValue(defaultValue: 450), or set conditions that cover all possible screen widths.

ResponsiveValue<double>(
                  context,
                  conditionalValues: [
                    Condition.equals(name: MOBILE, value: 450),
                    Condition.between(start: 800, end: 1100, value: 800),
                    Condition.between(start: 1000, end: 1200, value: 1000),
                  ],
                ).value
tsafundzic commented 1 year ago

Conditions are set in builder in MaterialApp. Same as yours.

JakobLichterfeld commented 9 months ago

I had the same issue.

You need to set the default value here:

ResponsiveScaledBox(
        width: ResponsiveValue<double>(
          context,
          defaultValue: 450, // default value to avoid _TypeError (type 'Null' is not a subtype of type 'double' in type cast)
          conditionalValues: [...],
        ).value,
        child: child
congnguyen-prismtech commented 4 months ago

When i met it in the first time, It has crashed my features when i updated to new version of this package, i took a while to found out the reason. i think the owner should add an assert or mark defaultValue required to inform for the developer know. Thanks