0

Hello everyone I am trying to build a reusable SliverList into which I just pass the sliver widgets as a List to display, and doubles for padding however currently getting this error

The code of the reusable widget I am trying to build

class ScrollScreen extends StatelessWidget {
  ScrollScreen({
    @required this.pageTitle,
    @required this.pageImage,
    @required this.widgets,
    this.color = Colors.white,
  });

  final String pageTitle;
  final String pageImage;
  final List<Widget> widgets;
  final Color color;

  @override
Widget build(BuildContext context) {
return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
  SliverBar(title: pageTitle, image: pageImage),
  SliverListLayout(widgets: widgets),
]);

}

Class of SliverListLayout

class SliverListLayout extends StatelessWidget {
final double paddingTB;
final double paddingLR;
final List<Widget> widgets;
SliverListLayout({
@required this.widgets,
this.paddingTB,
this.paddingLR,
});

@override
Widget build(BuildContext context) {
return SliverList(
  delegate: SliverChildListDelegate([
    ...addPadding(widgets, paddingTB, paddingLR), //add padding wraps widgets in a padding widget and returns them,, not the issue works fine elsewhere
  ]),
);
}

} StackTrace:

The following NoSuchMethodError was thrown building             
NotificationListener<KeepAliveNotification>:
The method '>=' was called on null.
Receiver: null
Tried calling: >=(0.0)

The relevant error-causing widget was
SliverList
lib\…\custom_widgets\sliverlist_layout.dart:15
When the exception was thrown, this was the stack
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      EdgeInsetsGeometry.isNonNegative
package:flutter/…/painting/edge_insets.dart:52
#2      new RenderPadding
package:flutter/…/rendering/shifted_box.dart:107
#3      Padding.createRenderObject
package:flutter/…/widgets/basic.dart:1652
#4      RenderObjectElement.mount

Flutter Doctor:

PS C:\Users\Usuario\Desktop\AppName> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Versión 10.0.19042.746], locale es-ES)
 
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[!] Android Studio (version 4.1.0)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.      
[√] VS Code (version 1.52.1)
[√] Connected device (1 available)

! Doctor found issues in 2 categories.
PS C:\Users\Usuario\Desktop\AppName> 

addPadding

List<Widget> addPadding(List<Widget> widgets,
    [double paddingTB = 25.0, double paddingLR = 25.0]) {
  for (int i = 0; i < widgets.length; i++) {
    widgets[i] = Padding(
      padding: EdgeInsets.fromLTRB(
        paddingLR,
        paddingTB,
        paddingLR,
        paddingTB,
      ),
      child: widgets[i],
    );
  }
  return widgets;
}

I paste the code from the SliverListLayout class (without the two extra parameters) and everything works fine. Also SliverBar returns a SliverAppBar, and it works fine. Any help is very appreciated.

1 Answer 1

0

I'm not sure how to reproduce your problem. I would recommend:

  1. Post a complete yet minimal code sample reproducing your problem
  2. Post the full console error stack trace
  3. Provide more info about your configuration (flutter doctor)

Here is a small code sample doing something simple, similar to (I think) what you try to achieve.

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
          body: ReusableSliverList(
        title: 'Testing my ReusableSliverList',
        widgetPadding: EdgeInsets.symmetric(horizontal: 32.0, vertical: 16.0),
        widgets: List.generate(10, (index) => Text('Widget $index')).toList(),
      )),
    ),
  );
}

class ReusableSliverList extends StatelessWidget {
  final String title;
  final List<Widget> widgets;
  final EdgeInsets widgetPadding;

  const ReusableSliverList({
    Key key,
    this.title,
    this.widgets,
    this.widgetPadding,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      physics: const BouncingScrollPhysics(),
      slivers: [
        SliverAppBar(title: Text(title)),
        SliverList(
          delegate: SliverChildListDelegate(
            widgets
                .map((widget) => Padding(padding: widgetPadding, child: widget))
                .toList(),
          ),
        ),
      ],
    );
  }
}

Full working code based on your question edit (with addPadding):

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: ScrollScreen(
          widgets: List.generate(10, (index) => Text('Widget $index')),
        ),
      ),
    ),
  );
}

class ScrollScreen extends StatelessWidget {
  ScrollScreen({
    // @required this.pageTitle,
    // @required this.pageImage,
    @required this.widgets,
    this.color = Colors.white,
  });

  // final String pageTitle;
  // final String pageImage;
  final List<Widget> widgets;
  final Color color;

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
//      SliverBar(title: pageTitle, image: pageImage),
      SliverListLayout(
        widgets: widgets,
        paddingLR: 16.0,
        paddingTB: 32.0,
      ),
    ]);
  }
}

class SliverListLayout extends StatelessWidget {
  final double paddingTB;
  final double paddingLR;
  final List<Widget> widgets;
  SliverListLayout({
    @required this.widgets,
    this.paddingTB,
    this.paddingLR,
  });

  @override
  Widget build(BuildContext context) {
    return SliverList(
      delegate: SliverChildListDelegate([
        ...addPadding(
          widgets,
          paddingTB,
          paddingLR,
        ),
      ]),
    );
  }

  List<Widget> addPadding(List<Widget> widgets,
      [double paddingTB = 25.0, double paddingLR = 25.0]) {
    for (int i = 0; i < widgets.length; i++) {
      widgets[i] = Padding(
        padding: EdgeInsets.fromLTRB(
          paddingLR,
          paddingTB,
          paddingLR,
          paddingTB,
        ),
        child: widgets[i],
      );
    }
    return widgets;
  }
}
8
  • that is exactly it however when I moved the code of SliverList to the separate and try to use it the error happens Commented Jan 30, 2021 at 1:02
  • Can you provide the full source code of your failing sample?
    – Thierry
    Commented Jan 30, 2021 at 2:10
  • what do you need? it is provided above apart from that there is only the addPadding method and the class SliverBar which returns the App Bar Commented Jan 30, 2021 at 2:46
  • Well, what I need is the failing part. I suspect it's your addPadding method since the error trace mentioned edgeInsetsGeometry. But, I'm not sure. I'll update answer with a full working code where I removed the parts you didn't provide: SliverBar and addPadding.
    – Thierry
    Commented Jan 30, 2021 at 7:26
  • thanks, i'll post addPadding and remove it to check just in case Commented Jan 30, 2021 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.