Home > other >  how to solve the error each child must lay out exactly once and invalid value
how to solve the error each child must lay out exactly once and invalid value

Time:07-21

i have been trying to make an app that has a listview and ontap it goes to details page and shows info that got from navigator (by a datamodel)here is the code i have tried everything in the internet but it still has this two errors (its not a bug)

code


import 'package:flutter/material.dart';
    import 'package:train/pages/dishdatamodel.dart';
    import 'package:train/pages/dishdetail.dart';
    import 'home.dart';

    class Favorites extends StatefulWidget {
      @override
         _FavoritesState createState() => _FavoritesState();
    }

    class _FavoritesState extends State<Favorites> {
      var itemcount =4;
      static List<String> dishname = ['MEXICAN BURGER','PASTA','DESERT','ICE CREAM','BIRIYANI','EGG BIRIYANI','CHICKEN BIRIYANI'];
      static List<String> dishprice = ['12\$', '20\$', '15\$', '10\$', '30\$', '35\$', '40\$'];
      static List urlImages = [
        'https://storage.googleapis.com/cms-storage-bucket/442072a900a8a1c2cc10.jpg''https://pixabay.com/photos/cake-piece-plate-dessert-pastry-1971552/',
        'https://pixabay.com/photos/cake-piece-plate-dessert-pastry-1971552/',
        'https://pixabay.com/photos/cake-piece-plate-dessert-pastry-1971552/',
        'https://pixabay.com/photos/cake-piece-plate-dessert-pastry-1971552/',
        'https://pixabay.com/photos/cake-piece-plate-dessert-pastry-1971552/',
        'https://pixabay.com/photos/cake-piece-plate-dessert-pastry-1971552/',
      ];


    final List<dishdatamodel>dishdata = List.generate(
          dishname.length,
          (index) =>dishdatamodel('${dishname[index]}', '${urlImages[index]}',
              '${dishname[index]}description.......', '${dishprice[index]}'));
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SingleChildScrollView(
            child: Column(
              children: [
                ListView.builder(
                    itemCount: dishdata.length,
                    itemBuilder: (BuildContext context,int index){
                  return Card(
                    color: Colors.deepOrange,
                    child: ListTile(
                      title: Text(dishdata[index].name),
                      trailing: Text(dishdata[index].price),
                      leading: ConstrainedBox(
                        constraints: BoxConstraints(
                          minHeight: 40,
                          minWidth: 40,
                          maxHeight: 50,
                          maxWidth: 50,
                        ),
                        child: Image.network(dishdata[index].imageurl),
                      ),
                      onTap: (){
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) => dishdetail(dishdatamode: dishdata[index],)));
                      },
                    ),
                  );
                })
              ],
            ),
          ),
        );
      }
    }

here is the errors

 ======== Exception caught by widgets library =======================================================
    The following RangeError was thrown building _BodyBuilder:
    RangeError (index): Invalid value: Not in inclusive range 0..5: 6
    
    The relevant error-causing widget was: 
      Scaffold file:///C:/Users/user/Desktop/android studio projets/train/lib/nav.dart:29:12
    When the exception was thrown, this was the stack: 
    #0      List.[] (dart:core-patch/growable_array.dart:177:60)
    #1      new _FavoritesState.<anonymous closure> (package:train/pages/favorites.dart:26:65)
    #2      new _GrowableList.generate (dart:core-patch/growable_array.dart:128:28)
    #3      new _FavoritesState (package:train/pages/favorites.dart:24:44)
    #4      Favorites.createState (package:train/pages/favorites.dart:8:36)
    
    ====================================================================================================
    
    ======== Exception caught by rendering library =====================================================
    The following assertion was thrown during performLayout():
    Each child must be laid out exactly once.
    
    The _ScaffoldLayout custom multichild layout delegate forgot to lay out the following child: 
      _ScaffoldSlot.body: RenderErrorBox#14191 NEEDS-LAYOUT NEEDS-PAINT
        parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body
        constraints: MISSING
        size: MISSING
    The relevant error-causing widget was: 
      Scaffold file:///C:/Users/user/Desktop/android studio projets/train/lib/nav.dart:29:12
    When the exception was thrown, this was the stack: 
    #0      MultiChildLayoutDelegate._callPerformLayout.<anonymous closure> (package:flutter/src/rendering/custom_layout.dart:246:11)
    #1      MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:259:8)
    #2      RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:402:14)
    #3      RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1634:7)
    #4      PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:884:18)
    ...
    The following RenderObject was being processed when the exception was fired: RenderCustomMultiChildLayoutBox#dc3f5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
    .  needs compositing
    .  parentData: <none> (can use size)
    .  constraints: BoxConstraints(w=392.7, h=850.9)
    .  size: Size(392.7, 850.9)
    RenderObject: RenderCustomMultiChildLayoutBox#dc3f5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
      needs compositing
      parentData: <none> (can use size)
      constraints: BoxConstraints(w=392.7, h=850.9)
      size: Size(392.7, 850.9)
    .  child 1: RenderErrorBox#14191 NEEDS-LAYOUT NEEDS-PAINT
    .    parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body
    .    constraints: MISSING
    .    size: MISSING
    .  child 2: RenderPositionedBox#34f21 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
    .    needs compositing
    .    parentData: offset=Offset(0.0, 83.6); id=_ScaffoldSlot.body (can use size)
    .    constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=711.3)
    .    size: Size(392.7, 711.3)
    .    alignment: center
    .    textDirection: ltr
    .    widthFactor: expand
    .    heightFactor: expand
    .  child 3: RenderConstrainedBox#67334 relayoutBoundary=up1
    .    needs compositing
    .    parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.appBar (can use size)
    .    constraints: BoxConstraints(w=392.7, 0.0<=h<=850.9)
    .    size: Size(392.7, 83.6)
    .    additionalConstraints: BoxConstraints(0.0<=w<=Infinity, 0.0<=h<=83.6)
    .    child: RenderSemanticsAnnotations#268b5 relayoutBoundary=up2
    .      needs compositing
    .      parentData: <none> (can use size)
    .      constraints: BoxConstraints(w=392.7, 0.0<=h<=83.6)
    .      semantic boundary
    .      size: Size(392.7, 83.6)
    .      child: RenderAnnotatedRegion<SystemUiOverlayStyle>#8f8ff relayoutBoundary=up3
    .        needs compositing
    .        parentData: <none> (can use size)
    .        constraints: BoxConstraints(w=392.7, 0.0<=h<=83.6)
    .        size: Size(392.7, 83.6)
    .        child: RenderPhysicalModel#6f052 relayoutBoundary=up4
    .          needs compositing
    .          parentData: <none> (can use size)
    .          constraints: BoxConstraints(w=392.7, 0.0<=h<=83.6)
    .          layer: PhysicalModelLayer#4e633
    .            engine layer: PhysicalShapeEngineLayer#9e72c
    .            elevation: 4.0
    .            color: MaterialColor(primary value: Color(0xffff5722))
    .          size: Size(392.7, 83.6)
    .          elevation: 4.0
    .          color: MaterialColor(primary value: Color(0xffff5722))
    .          shadowColor: MaterialColor(primary value: Color(0xffff5722))
    .          shape: BoxShape.rectangle
    .          borderRadius: BorderRadius.zero
    .  child 4: RenderSemanticsAnnotations#b44d0 relayoutBoundary=up1 NEEDS-PAINT
    .    needs compositing
    .    parentData: offset=Offset(0.0, 794.9); id=_ScaffoldSlot.bottomNavigationBar (can use size)
    .    constraints: BoxConstraints(w=392.7, 0.0<=h<=850.9)
    ..    size: Size(392.7, 56.0)
    ..    child: RenderPhysicalModel#627c2 relayoutBoundary=up2 NEEDS-PAINT
    ..      needs compositing
    ..      parentData: <none> (can use size)
    ..      constraints: BoxConstraints(w=392.7, 0.0<=h<=850.9)
    ..      layer: PhysicalModelLayer#26163
    ..        engine layer: PhysicalShapeEngineLayer#3df82
    ..        elevation: 8.0
    ..        color: Color(0xfffafafa)
    ..      size: Size(392.7, 56.0)
    ..      elevation: 8.0
    ..      color: Color(0xfffafafa)
    ..      shadowColor: Color(0xfffafafa)
    ..      shape: BoxShape.rectangle
    ..      borderRadius: BorderRadius.zero
    ..      child: _RenderInkFeatures#64e29 relayoutBoundary=up3 NEEDS-PAINT
    ..        parentData: <none> (can use size)
    ..        constraints: BoxConstraints(w=392.7, 0.0<=h<=850.9)
    ..        size: Size(392.7, 56.0)
    ..        child: RenderConstrainedBox#c08b0 relayoutBoundary=up4 NEEDS-PAINT
    ..          parentData: <none> (can use size)
    ..          constraints: BoxConstraints(w=392.7, 0.0<=h<=850.9)
    ..          size: Size(392.7, 56.0)
    ..          additionalConstraints: BoxConstraints(0.0<=w<=Infinity, 56.0<=h<=Infinity)
    ..  child 5: RenderStack#b0601 relayoutBoundary=up1
    ..    parentData: offset=Offset(376.7, 778.9); id=_ScaffoldSlot.floatingActionButton (can use ize)
    ..    constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=850.9)
    ..    size: Size(0.0, 0.0)
    ..    alignment: centerRight
    ..    textDirection: ltr
    ..    fit: loose
    ..    child 1: RenderTransform#7227d relayoutBoundary=up2
    ..      parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
    ..      constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=850.9)
    ..      size: Size(0.0, 0.0)
    ..      transform matrix: [0] 0.0,0.0,0.0,0.0
    1] 0.0,0.0,0.0,0.0
    2] 0.0,0.0,1.0,0.0
    [3] 0.0,0.0,0.0,1.0
    ..      origin: null
    ..      alignment: center
    ..      textDirection: ltr
    ..      transformHitTests: true
    ..      child: RenderTransform#1577a relayoutBoundary=up3
    ..        parentData: <none> (can use size)
    ..        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=850.9)
    ..        size: Size(0.0, 0.0)
    ..        transform matrix: [0] 0.7,0.7,0.0,0.0
    [1] -0.7,0.7,0.0,0.0
    [2] 0.0,0.0,1.0,0.0
    [3] 0.0,0.0,0.0,1.0
    ..        origin: null
    ..        alignment: center
    ..        textDirection: ltr
    ..        transformHitTests: true
    ====================================================================================================

CodePudding user response:

this error is because you didn't set a height for List view, simply you can wrap the list view in to a container and give it a height number, notice that if you gonna use context.size.height, it's better to use MediaQuery.of(context).size.height if it didn't work, try wrap in to a Expanded widget Hope it helps

CodePudding user response:

Wrap ListView.builder with Expanded widget, Also no need to use SingleChildScrollView.

Or You can use SingleChildScrollView with ListView.builder physics: NeverScrollableScrollPhysics(),

Or just render item inside Column like

body: SingleChildScrollView(
  physics: NeverScrollableScrollPhysics(),
  child: Column(
    children: [
      ...dishdata.map((e) => Card(
            color: Colors.deepOrange,
            child: ListTile(
              title: Text(e.name),
              trailing: Text(e.price),
              leading: ConstrainedBox(
                constraints: BoxConstraints(
                  minHeight: 40,
                  minWidth: 40,
                  maxHeight: 50,
                  maxWidth: 50,
                ),
                child: Image.network(e.imageurl),
              ),
              onTap: () {},
            ),
          )),
    ],
  ),
),
);

There are others error I can see from console like range error,

  • Related