gpt4 book ai didi

ios - 在 Flutter 中使用 ListWheelScrollView 时获取与图像相关的异常

转载 作者:行者123 更新时间:2023-12-05 06:57:18 28 4
gpt4 key购买 nike

在 iOS 上使用 ListWheelScrollView 显示图像小部件时,我遇到了一个特殊错误。它包含在 PageView 的一页中,并且在我最小化该应用程序之前它工作正常。如果应用程序在进入后台后恢复,然后我切换到包含 ScrollView 的页面(通过切换并在恢复后切换回来或在最小化前切换然后在恢复后切换回来),可见图像无法显示输出如下:

════════ Exception caught by image resource service════════════════════════════ The method 'toDouble' was called on null.Receiver: null Tried calling: toDouble()════════════════════════════════════════════════════════════════════════════════

下面是一个演示问题的简单示例:

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ListWheel Issue',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'ListWheelScrollview Bug'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _currentIndex = 0;
PageController _pageController;
List<Widget> get _tabs => [
ScrollScreen(),
Container(),
Container(),
];

@override
void initState() {
_pageController = PageController();
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: PageView(
controller: _pageController,
children: _tabs,
)),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.refresh), title: Text('tab1')),
BottomNavigationBarItem(icon: Icon(Icons.movie), title: Text('tab2')),
BottomNavigationBarItem(
icon: Icon(Icons.search), title: Text('tab3')),
],
currentIndex: _currentIndex,
onTap: (i) {
setState(() {
_currentIndex = i;
_pageController.animateToPage(i,
curve: Curves.easeOut, duration: Duration(milliseconds: 200));
});
},
),
);
}
}

class ScrollScreen extends StatefulWidget {
@override
_ScrollScreenState createState() => _ScrollScreenState();
}

class _ScrollScreenState extends State<ScrollScreen> {
var _exImage = AssetImage('assets/images/no_image.png');

@override
Widget build(BuildContext context) {
List<AssetImage> _images = [
_exImage,
_exImage,
_exImage,
];
return Center(
child: ListWheelScrollView.useDelegate(
itemExtent: 300,
childDelegate: ListWheelChildLoopingListDelegate(
children: _images
.map((e) => Center(
child: Image(
image: e,
)))
.toList())),
);
}
}

如有任何帮助,我们将不胜感激。

最佳答案

我找到了解决方案。

 @override
void didChangeDependencies() {
super.didChangeDependencies();
precacheImage(AssetImage('assets/icons/settings_icon.png'), context);
}

预缓存ListWheelScrollView中使用的图片后,错误不再出现

不知道 flutter 团队将来会不会修复它。

关于ios - 在 Flutter 中使用 ListWheelScrollView 时获取与图像相关的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64913965/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com