ListWheelScrollView 中的 Flutter Network Image 在 init 上不起作用
Flutter Network Image inside ListWheelScrollView not working on init
我在我的应用程序中使用 ListWheelScrollView
,我想在子项中显示一些 Image.network
。但这是不可能的。在第一次开始时,我总是得到错误:
Null check operator used on a null value
StackTrace 表明这是问题所在(来自 ListWheelScrollView):
@override
double get minScrollExtent => _minScrollExtent!;
double? _minScrollExtent;
知道问题出在哪里吗?我该如何解决?问题是,在第一次启动后,我可以 热加载 并且一切都按预期工作...
这是重现问题的整个项目:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
home: FirstScreen(),
);
}
}
class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
final items = [1, 2, 3, 4, 5, 6, 7, 8];
return Material(
child: GestureDetector(
onTap: () {},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 50),
child: ListWheelScrollView.useDelegate(
itemExtent: 300,
physics: FixedExtentScrollPhysics(),
perspective: 0.0015,
useMagnifier: false,
onSelectedItemChanged: (index) {
print(index);
},
childDelegate: ListWheelChildBuilderDelegate(
builder: (context, index) => Container(
height: 300,
width: 200,
color: Colors.red,
child: Image.network(
'https://flif.info/example-images/fish.png',
height: 160,
width: 160,
),
),
childCount: items.length,
),
),
),
),
);
}
}
对于遇到此问题的任何人,GitHub 上建议了一些临时解决方法:
The issue is due to the widget having no size. (loading a Network images for example). A placeolder with a size should avoid this issue.
FYI, I found a workaround: Use a FutureBuilder
. You can wrap your widget into it and it will show without the exception. You might consider using a SynchronousFuture
to make it not blink.
如前所述here,这将解决问题:
@override
double get minScrollExtent => _minScrollExtent;
double _minScrollExtent = 0.0;
@override
double get maxScrollExtent => _maxScrollExtent;
double _maxScrollExtent = 0.0;
我在我的应用程序中使用 ListWheelScrollView
,我想在子项中显示一些 Image.network
。但这是不可能的。在第一次开始时,我总是得到错误:
Null check operator used on a null value
StackTrace 表明这是问题所在(来自 ListWheelScrollView):
@override
double get minScrollExtent => _minScrollExtent!;
double? _minScrollExtent;
知道问题出在哪里吗?我该如何解决?问题是,在第一次启动后,我可以 热加载 并且一切都按预期工作...
这是重现问题的整个项目:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
home: FirstScreen(),
);
}
}
class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
final items = [1, 2, 3, 4, 5, 6, 7, 8];
return Material(
child: GestureDetector(
onTap: () {},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 50),
child: ListWheelScrollView.useDelegate(
itemExtent: 300,
physics: FixedExtentScrollPhysics(),
perspective: 0.0015,
useMagnifier: false,
onSelectedItemChanged: (index) {
print(index);
},
childDelegate: ListWheelChildBuilderDelegate(
builder: (context, index) => Container(
height: 300,
width: 200,
color: Colors.red,
child: Image.network(
'https://flif.info/example-images/fish.png',
height: 160,
width: 160,
),
),
childCount: items.length,
),
),
),
),
);
}
}
对于遇到此问题的任何人,GitHub 上建议了一些临时解决方法:
The issue is due to the widget having no size. (loading a Network images for example). A placeolder with a size should avoid this issue.
FYI, I found a workaround: Use a
FutureBuilder
. You can wrap your widget into it and it will show without the exception. You might consider using aSynchronousFuture
to make it not blink.
如前所述here,这将解决问题:
@override
double get minScrollExtent => _minScrollExtent;
double _minScrollExtent = 0.0;
@override
double get maxScrollExtent => _maxScrollExtent;
double _maxScrollExtent = 0.0;