Reputation: 21
Why when I add conditions (pictureUrl != null) to one of the application files does the error occur in other application files using the same widget? Please explain the mechanism of the error. Apparently this is due to the reuse of widgets?
Condition added here:
class PListView extends StatelessWidget {
const PListView({super.key});
@override
Widget build(BuildContext context) {
double w = MediaQuery.of(context).size.width - 16;
final ScrollController controller = ScrollController();
double offset = 0;
bool end = false;
bool oneTime = true;
controller.addListener(() async {
if (controller.position.pixels >=
controller.position.maxScrollExtent - 100 &&
!end &&
oneTime) {
offset = controller.offset;
oneTime = false;
end = await BlocProvider.of<PCubit>(context).addNews(2);
oneTime = true;
if (!end) {
controller.jumpTo(offset);
}
}
});
return BlocBuilder<PCubit, PState>(
buildWhen: (previous, current) => current.status.isSuccess,
builder: (context, state) {
if (state.status.isSuccess) {
List<News> newsList = state.newsList;
return RefreshIndicator(
onRefresh: () async {
await BlocProvider.of<PCubit>(context).fetchNews(2);
},
child: ListView.builder(
itemCount: newsList.length,
itemBuilder: (BuildContext context, int index) {
News newsItem = newsList[index];
String? pictureUrl = newsItem.announcePictureUrl;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: InkWell(
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
(pictureUrl != null)
? Hero(
tag: newsItem.code,
child: SizedBox(
height: w * 9 / 16,
width: w,
child: ClipRRect(
borderRadius:
const BorderRadius.vertical(
top: Radius.circular(10),
bottom: Radius.circular(0)),
child: FittedBox(
alignment: Alignment.topCenter,
clipBehavior: Clip.hardEdge,
fit: BoxFit.cover,
child: Picture(
url: pictureUrl,
radius: 0,
placeholderWidth: w,
placeholderHeight: w * 9 / 16,
),
),
),
),
)
: SizedBox(
height: w * 1 / 5,
width: w,
child: const ClipRRect(
borderRadius: BorderRadius.vertical(
top: Radius.circular(10),
bottom: Radius.circular(0)),
child:
ColoredBox(color: Colors.green)),
),
The error in FittedBox occurs here, when url == null :
Widget build(BuildContext context) {
return Hero(
tag: code,
child: SizedBox(
width: w,
child: ClipRRect(
borderRadius: const BorderRadius.vertical(
top: Radius.circular(10), bottom: Radius.circular(0)),
child: FittedBox(
alignment: Alignment.topCenter,
clipBehavior: Clip.hardEdge,
fit: BoxFit.fitWidth,
child: Picture(
url: url,
radius: 0,
placeholderWidth: w,
placeholderHeight: w * 9 / 16,
),
),
),
),
);
}
Widget code:
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(radius)),
child: (url != null) ? CachedNetworkImage(
imageUrl: url!,
errorWidget: (context, url, error) => const Icon(Icons.error),
placeholder: (context, url) =>
(placeholderWidth != null && placeholderHeight != null)
? SizedBox(
width: placeholderWidth,
height: placeholderHeight,
child: PLoader(
maxIconSize: loaderMaxSize,
durationMilliseconds: loaderDurationMillseconds),
)
: PLoader(
maxIconSize: loaderMaxSize,
durationMilliseconds: loaderDurationMillseconds),
width: width,
height: height,
fit: fit,
)
:
Image.asset('assets/default.jpg'),
);
}
If you remove (pictureUrl != null) in the first file, the error in other files disappears.
the error:
════════ Exception caught by rendering library ═════════════════════════════════
The following assertion was thrown during performLayout():
'package:flutter/src/rendering/box.dart': Failed assertion: line 320 pos 12: 'width > 0.0': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=2_bug.yml
The relevant error-causing widget was:
FittedBox FittedBox:file:.../lib/view/article.dart:88:18
Upvotes: 2
Views: 64