best-flutter / flutter_swiper

The best swiper for flutter , with multiple layouts, infinite loop. Compatible with Android & iOS.
MIT License
3.51k stars 717 forks source link

The method '-' was called on null error on initial load. Error resolves on hot reload. #139

Open Shantaviae opened 5 years ago

Shantaviae commented 5 years ago

When I run my app with the Swiper configured as such I get the error below. This error goes not throw (or "fixes itself") when I reload the app. If I change layout to SwiperLayout.DEFAULT I do not get this error.

LimitedBox( maxHeight: cardSize.height, child: Swiper( itemBuilder: (BuildContext context, int index) { if (goalsRepository.goals.length > 0) { Goal goal = goalsRepository.goals[index]; return GoalCard( goal: goal, cardSize: cardSize, key: ValueKey(goal.id)); } return null; }, itemCount: goalsRepository.goals.length, layout: SwiperLayout.STACK, itemWidth: cardSize.width, itemHeight: cardSize.height, controller: SwiperController(), onIndexChanged: (index) => goalsRepository.updateViewingGoal(index), ), ),

ERROR Log:

I/flutter (12736): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (12736): The following NoSuchMethodError was thrown building Swiper(state: _SwiperState#677cd): I/flutter (12736): The method '-' was called on null. I/flutter (12736): Receiver: null I/flutter (12736): Tried calling: -(349.7142857142857) I/flutter (12736): I/flutter (12736): User-created ancestor of the error-causing widget was: I/flutter (12736): LimitedBox file:///Users/shantaviaewynn/Projects/nudged/lib/screens/home_screen.dart:28:15 I/flutter (12736): I/flutter (12736): When the exception was thrown, this was the stack: I/flutter (12736): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) I/flutter (12736): #1 _StackViewState._updateValues (package:flutter_swiper/src/swiper.dart:864:36) I/flutter (12736): #2 _StackViewState.didUpdateWidget (package:flutter_swiper/src/swiper.dart:874:5) I/flutter (12736): #3 StatefulElement.update (package:flutter/src/widgets/framework.dart:4103:58) I/flutter (12736): #4 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #6 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #7 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #9 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #11 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4852:32) I/flutter (12736): #12 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5243:17) I/flutter (12736): #13 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #14 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #18 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #19 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #20 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #21 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #22 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #23 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #24 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #25 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #26 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #28 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4852:32) I/flutter (12736): #29 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5243:17) I/flutter (12736): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #31 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #32 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #33 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #35 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #36 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #37 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #38 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #40 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #41 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #43 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #45 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #46 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #47 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #48 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #49 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #50 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #51 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #52 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #53 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #54 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #55 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #56 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #57 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #59 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #60 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #61 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #62 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #63 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #64 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #65 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #66 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #67 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #68 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #69 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #70 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #72 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #73 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #74 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #75 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #76 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #77 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #78 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #79 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #80 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #81 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #82 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #83 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #84 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #85 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #86 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #87 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #88 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #89 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #90 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #91 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #92 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #93 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #94 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #95 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #96 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #97 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #98 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #99 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #100 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #101 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #102 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #103 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #104 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #105 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #106 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #107 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #108 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #109 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #110 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #111 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #112 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #113 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #114 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #115 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #116 _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:94:11) I/flutter (12736): #117 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #118 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #119 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #120 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #121 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #122 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5) I/flutter (12736): #123 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #124 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #125 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #126 StatelessElement.update (package:flutter/src/widgets/framework.dart:4016:5) I/flutter (12736): #127 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #128 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14) I/flutter (12736): #129 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #130 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #131 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #132 ProxyElement.update (package:flutter/src/widgets/framework.dart:4254:5) I/flutter (12736): #133 Element.updateChild (package:flutter/src/widgets/framework.dart:2893:15) I/flutter (12736): #134 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16) I/flutter (12736): #135 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5) I/flutter (12736): #136 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2348:33) I/flutter (12736): #137 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:760:20) I/flutter (12736): #138 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:280:5) I/flutter (12736): #139 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1033:15) I/flutter (12736): #140 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:975:9) D/EGL_emulation(12736): eglMakeCurrent: 0x9f405480: ver 3 0 (tinfo 0x9f4033c0) I/flutter (12736): #141 SchedulerBinding.scheduleWarmUpFrame. (package:flutter/src/scheduler/binding.dart:784:7) I/flutter (12736): #150 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19) I/flutter (12736): #151 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5) I/flutter (12736): #152 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12) I/flutter (12736): (elided 8 frames from package dart:async and package dart:async-patch) I/flutter (12736): I/flutter (12736):

mario202k commented 5 years ago

Just add return goalsRepository.goals.isNotEmpty? Swiper(.... https://github.com/best-flutter/flutter_swiper/issues/47#issuecomment-540232690

Shantaviae commented 5 years ago

The goals list is initialized prior to the build and I've tested that it has content. I'm pretty sure its an issue specifically with the stack layout because when I go back to the default layout (without changing anything else) this error isn't thrown.

mario202k commented 5 years ago

No. It's not the culprit(stack layout ) I know that changing layout (SwiperLayout.TINDER) occured an error unless the default. You must test your list (itemCount) before !!!!! Swiper. Not as you've done into the itemBuilder.

itemBuilder: (BuildContext context, int index) { if (goalsRepository.goals.length > 0) {

Shantaviae commented 5 years ago

Hi,

I refactored my code to clean it up a bit. Let me know if I've still misinterpreted your suggestion. It's still throwing the same error as above when I run my app in debug mode (it doesn't when I do a hot reload). When I change the bold line to layout: SwiperLayout.DEFAULT, and hard restart the app in debug mode, I do not get an error. That's why I was concerned it's an issue with STACK layout. From the documentation, they don't appear to require different parameters, so I'm really stuck on how to figure out what's wrong with my code. Thanks for your help by the way!


class CustomSwiper extends StatelessWidget {
  GoalsRepository goalsRepository;
  Size cardSize;
  CustomSwiper({@required this.goalsRepository, @required this.cardSize});

  @override
  Widget build(BuildContext context) {
    print('number of goals ${goalsRepository.goals.length.toString()}');
    if (goalsRepository.goals.length > 0) {
      return LimitedBox(
        maxHeight: cardSize.height,
        child: Swiper(
          itemBuilder: (BuildContext context, int index) {
            Goal goal = goalsRepository.goals[index];
            return GoalCard(
                goal: goal, cardSize: cardSize, key: ValueKey(goal.id));
          },
          itemCount: goalsRepository.goals.length,
          layout: SwiperLayout.STACK,
          itemWidth: cardSize.width,
          itemHeight: cardSize.height,
          controller: SwiperController(),
          onIndexChanged: (index) => goalsRepository.updateViewingGoal(index),
        ),
      );
    } else {
      return LimitedBox(
        maxHeight: cardSize.height,
        child: CircularProgressIndicator(),
      );
    }
  }
}
mario202k commented 5 years ago

I would change limitedBox by ConstrainedBox or SizedBox Because LimitedBox : A box that limits its size only when it's unconstrained whereas Swiper have itemWidth and itemHeight. I've spent days on that issue If the data from the internet why don't you use StreamBuilder? Take a sneak peek on my code.

StreamBuilder( stream: slides, initialData: [], builder: (context, AsyncSnapshot snap) { List slideList = snap.data.toList(); if (slideList.isNotEmpty) { slideList.forEach((f) { events.add(MonEvent.fromMap(f)); }); } return Hero( tag: '123', child: slideList.isNotEmpty ? Swiper( itemBuilder: (BuildContext context, int index) { return Image.network( slideList[index]['image'], fit: BoxFit.fill, ); }, itemCount: slideList.length, pagination: SwiperPagination(), control: SwiperControl(), onTap: (index) { Navigator.pushNamed(context, '/details', arguments: events[index]); }, itemWidth: MediaQuery.of(context).size.width - MediaQuery.of(context).size.width 0.1, itemHeight: MediaQuery.of(context).size.height - MediaQuery.of(context).size.height 0.1, layout: SwiperLayout.TINDER, loop: true, outer: true, autoplay: true, autoplayDisableOnInteraction: false, ) : Center( child: CircularProgressIndicator(), ), ); })`

FronnieSW commented 5 years ago

This rly might be a plugins bug. The problem is that at one frame the _swiperHeight is null so it crashes. I solved this by creating my own layout, that looks almost exactly same as the STACK one.

layout: SwiperLayout.CUSTOM,
 customLayoutOption: new CustomLayoutOption(
          startIndex: ITEMS_COUNT - 3,
          stateCount: 5
      ).addScale([
          0.7,
          0.8,
          0.9,
          1.0,
          1.0,
      ], Alignment.center).addTranslate([
          Offset(0.0, -45.0),
          Offset(0.0, -45.0),
          Offset(0.0, -20.0),
          Offset(0.0, 0.0),
          Offset(0.0, ITEM_HEIGHT),
      ]).addOpacity([
          0.0,
          0.75,
          0.95,
          1.0,
          0.0,
      ]),

If you use MediaQuery.of(context).size.height * X for itemHeight, then change values in .addTransalte for % values as well..

for example:

_maxHeight = MediaQuery.of(context).size.height;
_itemHeight = _maxHeight  * 0.4;

.addTranslate([
         new Offset(0.0, -_maxHeight * 0.1),
         new Offset(0.0, -_maxHeight * 0.085),
         new Offset(0.0, -_maxHeight * 0.04),
         new Offset(0.0, 0.0),
         new Offset(0.0, _itemHeight),
       ])