lianyagang / flutter_swiper_null_safety Goto Github PK
View Code? Open in Web Editor NEW根据大佬https://github.com/best-flutter/flutter_swiper修改到空安全swiper
License: MIT License
根据大佬https://github.com/best-flutter/flutter_swiper修改到空安全swiper
License: MIT License
======== Exception caught by foundation library ====================================================
The following _CastError was thrown while dispatching notifications for SwiperController:
Null check operator used on a null value
Flutter 2.2.1版本下:
每滑动一页就报一次异常
此问题怎么解决呢?
E/flutter (30989): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (30989): At this point the state of the widget's element tree is no longer stable.
E/flutter (30989): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
E/flutter (30989): #0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3990:9)
E/flutter (30989): #1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4004:6)
E/flutter (30989): #2 Element.visitAncestorElements (package:flutter/src/widgets/framework.dart:4089:12)
E/flutter (30989): #3 Notification.dispatch (package:flutter/src/widgets/notification_listener.dart:83:13)
E/flutter (30989): #4 ScrollActivity.dispatchScrollEndNotification (package:flutter/src/widgets/scroll_activity.dart:104:63)
E/flutter (30989): #5 ScrollPosition.didEndScroll (package:flutter/src/widgets/scroll_position.dart:907:15)
E/flutter (30989): #6 ScrollPosition.beginActivity (package:flutter/src/widgets/scroll_position.dart:876:9)
E/flutter (30989): #7 ScrollPositionWithSingleContext.beginActivity (package:flutter/src/widgets/scroll_position_with_single_context.dart:114:11)
E/flutter (30989): #8 ScrollPositionWithSingleContext.goIdle (package:flutter/src/widgets/scroll_position_with_single_context.dart:129:5)
E/flutter (30989): #9 ScrollPositionWithSingleContext.jumpTo (package:flutter/src/widgets/scroll_position_with_single_context.dart:195:5)
E/flutter (30989): #10 ScrollPositionWithSingleContext.animateTo (package:flutter/src/widgets/scroll_position_with_single_context.dart:177:7)
E/flutter (30989): #11 PageController.animateToPage (package:flutter/src/widgets/page_view.dart:201:21)
E/flutter (30989): #12 _TransformerPageViewState.onChangeNotifier (package:flutter_swiper_null_safety/src/transformer_page_view/transformer_page_view.dart:590:12)
E/flutter (30989): #13 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:308:24)
E/flutter (30989): #14 IndexController.next (package:flutter_swiper_null_safety/src/transformer_page_view/index_controller.dart:30:5)
E/flutter (30989): #15 _SwiperTimerMixin._onTimer (package:flutter_swiper_null_safety/src/swiper.dart:369:18)
E/flutter (30989): #16 _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (30989): #17 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (30989): #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter (30989): #19 _CustomZone.bindUnaryCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1281:26)
E/flutter (30989): #20 _rootRunUnary (dart:async/zone.dart:1444:13)
E/flutter (30989): #21 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (30989): #22 _CustomZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1265:26)
E/flutter (30989): #23 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter (30989): #24 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter (30989): #25 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
自动滚动,每滚动一次,报一次错。这个咋回事?
Case:
I am getting this error when my swiper scaffold is in app background.
[*] I am using smooth_page_indicator for showing custom dots pagination
Initially i load the main page (which has the swiper) based on conditions i move to some other page, in the background i am getting these errors/exceptions. Only when swiper is in background of flutter app i get this kind of exception.
Any idea to solve !!!
Error: Null check operator used on a null value
Exception Caught:
======== Exception caught by foundation library ====================================================
The following _CastError was thrown while dispatching notifications for SwiperController:
Null check operator used on a null value
Swiper(
autoplay: true,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index) {
return swiperList[index];
},
itemCount: swiperList.length,
)
视图加载之后autoplay没生效,需要手动滑动之后才会生效。
每次滚动之前的组件都会重新加载,轻量化的还好
我3个组件,文字,视频,按钮,滑动几次后卡的很呀,我觉得大概问题是重新加载但是没有销毁之前的组件导致的
每次滚动组件会重新加载,
不销毁之前的组件
这两个问题能不能修复一下
希望就是不要重新加载组件,需要的时候自己调用重绘
When the exception was thrown, this was the stack:
#0 TransformerPageController.page (package:flutter_swiper_null_safety/src/transformer_page_view/transformer_page_view.dart:171:9)
#1 _PageIndicatorState._createPainer (package:flutter_swiper_null_safety/src/flutter_page_indicator/flutter_page_indicator.dart:235:40)
#2 _PageIndicatorState.build (package:flutter_swiper_null_safety/src/flutter_page_indicator/flutter_page_indicator.dart:250:18)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
--- lib/src/transformer_page_view/transformer_page_view.dart (revision 1365b80)
+++ lib/src/transformer_page_view/transformer_page_view.dart (revision 6f7c83ab0aef1ae40f808b5e2a7d6dd123337f49)
@@ -143,8 +143,8 @@
return renderIndex;
}
static _getRenderPageFromRealPage(
@@ -363,7 +363,7 @@
double position = 0;
double page = _pageController.realPage ?? 0;
double page = _pageController.realPage;
if (_transformer != null && _transformer!.reverse) {
position = page - index;
升级后提示
WidgetsBinding.instance!.addPostFrameCallback(_getSize);
使用DotSwiperPaginationBuilder时m,size和activeSize设置的一样大小,但是视觉上感觉不一样大,是有做缩放吗
如果大点的图片会导致轮播的效果割裂,而且如果源是错误的网络图片资源,开了自动开始和循环后会,flutter框架会不停的抛出资源错误,影响调试,pub上面有几个维护至空安全的库都未能解决这个问题
I have a working code before null safety flutter upgrade. But after the migration, the same code doesn't work.
I had a simple horizontal swipe card, but now something force the swipe to stay on the first position. When I remove didChangeDependencies
(function I use to load when data change) the swipe is OK. I think when data is load by didChangeDependencies
it refresh new Swiper.children( and force to return always to first index position.
But I can't do without didChangeDependencies
, how can I do ?
Here is the package https://pub.dev/packages/flutter_swiper_null_safety/example Here is my code:
@override
void didChangeDependencies() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final MyInheritedWidgetState state = MyInheritedWidget.of(context);
}
new Swiper.children(
viewportFraction: 0.8,
scale: 0.6,
autoplay: false,
loop: false,
control: new SwiperControl(
size: 25.0,
color: Color(0xffff9a7b),
disableColor: Colors.transparent ,
padding: const EdgeInsets.all (5.0),
),
children: <Widget>[
Card1()
Card2()
Card3()
]
flutter: ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
flutter: The following _CastError was thrown while dispatching notifications for SwiperController:
flutter: Null check operator used on a null value
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0 ScrollPosition.viewportDimension (package:flutter/src/widgets/scroll_position.dart:172:53)
flutter: #1 _PagePosition.getPixelsFromPage (package:flutter/src/widgets/page_view.dart:382:19)
flutter: #2 PageController.animateToPage (package:flutter/src/widgets/page_view.dart:197:16)
flutter: #3 _TransformerPageViewState.onChangeNotifier (package:flutter_swiper_null_safety/src/transformer_page_view/transformer_page_view.dart:590:12)
flutter: #4 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:243:25)
flutter: #5 IndexController.next (package:flutter_swiper_null_safety/src/transformer_page_view/index_controller.dart:30:5)
flutter: #6 _SwiperTimerMixin._onTimer (package:flutter_swiper_null_safety/src/swiper.dart:369:18)
flutter: (elided 10 frames from class _RawReceivePortImpl, class _Timer, and dart:async)
flutter:
flutter: The SwiperController sending notification was:
flutter: Instance of 'SwiperController'
初始化Swpier时,如果images长度为0,当网络请求回来时刷新时,images长度>0,此时就会出现快速滑动现象。
这个问题出现的原因是pageController的初始化initialPage=kMiddleValue
一个很大的数,,但此时的itemCount=0。
解决:
在 transformer_page_view.dart 文件下
static int _getRealIndexFromRenderIndex(
int index, bool loop, int itemCount, bool reverse) {
int result = reverse ? (itemCount - index - 1) : index;
if (loop && itemCount > 0) { // 新增itemCount > 0判断
result += kMiddleValue;
}
return result;
}
首次加载完两个页面,然后切换到第二个页面的时候会出现滑不动,再次滑动就可以,感觉像是丢失焦点了
Error at
I/flutter (26103): ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (26103): The following assertion was thrown during a scheduler callback:
I/flutter (26103): This widget has been unmounted, so the State no longer has a context (and should be considered
I/flutter (26103): defunct).
I/flutter (26103): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if
I/flutter (26103): the State is still active.
I/flutter (26103):
I/flutter (26103): When the exception was thrown, this was the stack:
I/flutter (26103): #0 State.context.<anonymous closure>
package:flutter/…/widgets/framework.dart:1088
I/flutter (26103): #1 State.context
package:flutter/…/widgets/framework.dart:1094
I/flutter (26103): #2 _TransformerPageViewState._onGetSize
package:flutter_swiper_null_safety/…/transformer_page_view/transformer_page_view.dart:444
I/flutter (26103): #3 SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1144
I/flutter (26103): #4 SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1090
I/flutter (26103): #5 SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:998
I/flutter (26103): #9 _invoke (dart:ui/hooks.dart:161:10)
I/flutter (26103): #10 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:253:5)
I/flutter (26103): #11 _drawFrame (dart:ui/hooks.dart:120:31)
I/flutter (26103): (elided 3 frames from dart:async)
I/flutter (26103): ════════════════════════════════════════════════════════════════════════════════════════════════════
dependencies: flutter_swiper_null_safety: ^1.0.3还没发吗?
body: Column(
children: [
Text('test flutter swiper'),
Container(
child: Swiper(
itemBuilder: (BuildContext context, int index) {
return Image.network(
"https://via.placeholder.com/350x150",
fit: BoxFit.fill,
);
},
itemCount: 3,
pagination: SwiperPagination(),
control: SwiperControl(),
),
)
],
)
报错Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderViewport#8c7d1 NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
package:flutter/…/rendering/box.dart:1
Failed assertion: line 1929 pos 12: 'hasSize'
After upgrade of flutter to a more recent version the slider stops sliding in web/html when used without "control". I use it as follows:
Swiper(
itemBuilder: (BuildContext context, int index) {
switch (index) {
case 0:
return InfoPage1();
case 1:
return InfoPage2();
...
case 6:
return InfoPage7();
default:
return const Padding(padding: EdgeInsets.all(12));
}
},
itemCount: 7,
scale: 0.9,
pagination: const SwiperPagination(
builder: DotSwiperPaginationBuilder(
size: 18.0,
activeSize: 18.0,
space: 9.0,
color: Colors.black12)),
),
output of flutter doctor is:
/usr/local/Caskroom/flutter/2.5.3/flutter/bin/flutter doctor --verbose
[✓] Flutter (Channel stable, 2.10.0, on macOS 12.2 21D49 darwin-x64, locale de-DE)
• Flutter version 2.10.0 at /usr/local/Caskroom/flutter/2.5.3/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 5f105a6ca7 (8 days ago), 2022-02-01 14:15:42 -0800
• Engine revision 776efd2034
• Dart version 2.16.0
• DevTools version 2.9.2
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Applications/android-sdk
• Platform android-31, build-tools 30.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
[✓] VS Code (version 1.63.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.27.0
[✓] Connected device (1 available)
• Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Any idea on how to fix it? Before a "mouse click and move left/right" initiates a "slide" - now it doesn't notice the mouse click.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.