Search code examples
flutterfirebaselistviewgoogle-cloud-firestorestream-builder

StreamBuilder<QuerySnapshot> is giving error


I am Creating a delivery app in which if the delivery is 2 km far it will show the order to the driver but I don't know why it is giving error.

This error is most probably caused by StreamBuilder. This is made using Flutter. I have used Firebase-Firestore as my data base.

My Code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:homekitchen_delivery_app_new/globals.dart';
import 'package:homekitchen_delivery_app_new/models/DriversRequest.dart';

class DemoNearMe extends StatefulWidget {
  const DemoNearMe({super.key});

  @override
  State<DemoNearMe> createState() => _DemoNearMeState();
}

class _DemoNearMeState extends State<DemoNearMe> {
  Placemark? placemark;
  List positions = [];
  Position? position;
  List<Placemark>? placemarks;
  getCurrentLocation() async {
    await Geolocator.requestPermission().whenComplete(() async {
      Position newPos = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.high);
      position = newPos;
      placemarks = await placemarkFromCoordinates(
          position!.latitude, position!.longitude);
      Placemark pMark = placemarks![0];
      placemark = pMark;
    });
    return [placemark, position!.latitude, position!.longitude];
  }

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      positions = await getCurrentLocation();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance
              .collection("driverRequest")
              .where("booked", isEqualTo: false)
              .where("orederType", isEqualTo: "demo")
              .where("city", isEqualTo: driverData!.city)
              .snapshots(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(child: CircularProgressIndicator());
            }
            return snapshot.hasData
                ? ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.data!.docs.length,
                    itemBuilder: (context, index) {
                      final data = snapshot.data!.docs[index].data();
                      DriverRequest driverRequest =
                          DriverRequest.fromJson(data as Map<String, dynamic>);
                      double distanceBetween = Geolocator.distanceBetween(
                              positions[1],
                              positions[2],
                              driverRequest.sellerLat,
                              driverRequest.sellerLng) /
                          1000;
                      if (distanceBetween <= 2) {
                        return Padding(
                          padding: const EdgeInsets.all(20.0),
                          child: Material(
                            elevation: 40,
                            child: Container(
                              child: InkWell(
                                onTap: () {
                                  // Navigator.push(context, MaterialPageRoute(builder: (c) => ))
                                },
                                child: Column(
                                  children: [
                                    ListTile(
                                      leading: CircleAvatar(
                                        backgroundImage: NetworkImage(
                                            driverRequest.sellerProfilePic),
                                      ),
                                      title: Text(driverRequest.sellerName),
                                      subtitle: Text(
                                          '${distanceBetween.toStringAsFixed(2)} km'),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        );
                      } else {
                        return Container();
                      }
                    },
                  )
                : Container(
                    height: 0.0,
                  );
          }),
    );
  }
}

Error:

Restarted application in 3,940ms.

════════ Exception caught by rendering library ═════════════════════════════════
The following assertion was thrown during performLayout():
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.

This probably means that it is a render object that tries to be as big as possible, but it was put inside another render object that allows its children to pick their own size.
The nearest ancestor providing an unbounded height constraint is: _RenderSingleChildViewport#b1d96 relayoutBoundary=up10 NEEDS-LAYOUT NEEDS-COMPOSITING-BITS-UPDATE
The constraints that applied to the RenderCustomMultiChildLayoutBox were: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
The exact size it was given was: Size(392.7, Infinity)

See https://flutter.dev/docs/development/ui/layout/box-constraints for more information.

The relevant error-causing widget was
Scaffold
When the exception was thrown, this was the stack
#0      RenderBox.debugAssertDoesMeetConstraints.<anonymous closure>
#1      RenderBox.debugAssertDoesMeetConstraints
#2      RenderBox.size=.<anonymous closure>
#3      RenderBox.size=
#4      RenderCustomMultiChildLayoutBox.performLayout
#5      RenderObject.layout
#6      RenderBox.layout
#7      RenderProxyBoxMixin.performLayout
#8      RenderObject.layout
#9      RenderBox.layout
#10     RenderProxyBoxMixin.performLayout
#11     _RenderCustomClip.performLayout
#12     RenderObject.layout
#13     RenderBox.layout
#14     _RenderSingleChildViewport.performLayout
#15     RenderObject.layout
#16     RenderBox.layout
#17     RenderProxyBoxMixin.performLayout
#18     RenderObject.layout
#19     RenderBox.layout
#20     RenderProxyBoxMixin.performLayout
#21     RenderObject.layout
#22     RenderBox.layout
#23     RenderProxyBoxMixin.performLayout
#24     RenderObject.layout
#25     RenderBox.layout
#26     RenderProxyBoxMixin.performLayout
#27     RenderObject.layout
#28     RenderBox.layout
#29     RenderProxyBoxMixin.performLayout
#30     RenderObject.layout
#31     RenderBox.layout
#32     RenderProxyBoxMixin.performLayout
#33     RenderObject.layout
#34     RenderBox.layout
#35     RenderProxyBoxMixin.performLayout
#36     RenderObject.layout
#37     RenderBox.layout
#38     RenderProxyBoxMixin.performLayout
#39     RenderCustomPaint.performLayout
#40     RenderObject.layout
#41     RenderBox.layout
#42     RenderProxyBoxMixin.performLayout
#43     RenderObject.layout
#44     RenderBox.layout
#45     MultiChildLayoutDelegate.layoutChild
#46     _ScaffoldLayout.performLayout
#47     MultiChildLayoutDelegate._callPerformLayout
#48     RenderCustomMultiChildLayoutBox.performLayout
#49     RenderObject._layoutWithoutResize
#50     PipelineOwner.flushLayout
#51     RendererBinding.drawFrame
#52     WidgetsBinding.drawFrame
#53     RendererBinding._handlePersistentFrameCallback
#54     SchedulerBinding._invokeFrameCallback
#55     SchedulerBinding.handleDrawFrame
#56     SchedulerBinding._handleDrawFrame
#57     _invoke (dart:ui/hooks.dart:145:13)
#58     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#59     _drawFrame (dart:ui/hooks.dart:112:31)
The following RenderObject was being processed when the exception was fired: RenderCustomMultiChildLayoutBox#8b10f relayoutBoundary=up13 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
RenderObject: RenderCustomMultiChildLayoutBox#8b10f relayoutBoundary=up13 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
    parentData: <none> (can use size)
    constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
    size: Size(392.7, Infinity)
    child 1: RenderPositionedBox#9b587 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body
        constraints: MISSING
        size: MISSING
        alignment: Alignment.center
        textDirection: ltr
        widthFactor: expand
        heightFactor: expand
        child: RenderSemanticsAnnotations#9cd70 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: offset=Offset(0.0, 0.0)
            constraints: MISSING
            size: MISSING
            child: RenderConstrainedBox#6da8f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                parentData: <none>
                constraints: MISSING
                size: MISSING
                additionalConstraints: BoxConstraints(36.0<=w<=Infinity, 36.0<=h<=Infinity)
                child: RenderCustomPaint#b87c0 NEEDS-LAYOUT NEEDS-PAINT
                    parentData: <none>
                    constraints: MISSING
                    size: MISSING
                    painter: _CircularProgressIndicatorPainter#44b11()
    child 2: RenderStack#7e46e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.floatingActionButton
        constraints: MISSING
        size: MISSING
        alignment: Alignment.centerRight
        textDirection: ltr
        fit: loose
        child 1: RenderTransform#f9ee2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: not positioned; offset=Offset(0.0, 0.0)
            constraints: MISSING
            size: MISSING
            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: Alignment.center
            textDirection: ltr
            transformHitTests: true
            child: RenderTransform#cb5bb NEEDS-LAYOUT NEEDS-PAINT
                parentData: <none>
                constraints: MISSING
                size: MISSING
                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: Alignment.center
                textDirection: ltr
                transformHitTests: true
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
_RenderInkFeatures object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
RenderPhysicalModel object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════
W/DynamiteModule(24486): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(24486): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(24486): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.

════════ Exception caught by rendering library ═════════════════════════════════
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
_RenderInkFeatures object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
RenderPhysicalModel object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderRepaintBoundary#9998e NEEDS-LAYOUT NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
Failed assertion: line 2009 pos 12: 'hasSize'

The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════

When I wrap StreamBuilder with column it gives error:

Restarted application in 3,092ms.

════════ Exception caught by rendering library ═════════════════════════════════
The following assertion was thrown during performLayout():
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.

This probably means that it is a render object that tries to be as big as possible, but it was put inside another render object that allows its children to pick their own size.
The nearest ancestor providing an unbounded height constraint is: _RenderSingleChildViewport#54eba relayoutBoundary=up10 NEEDS-LAYOUT NEEDS-COMPOSITING-BITS-UPDATE
The constraints that applied to the RenderCustomMultiChildLayoutBox were: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
The exact size it was given was: Size(392.7, Infinity)

See https://flutter.dev/docs/development/ui/layout/box-constraints for more information.

The relevant error-causing widget was
Scaffold
When the exception was thrown, this was the stack
#0      RenderBox.debugAssertDoesMeetConstraints.<anonymous closure>
#1      RenderBox.debugAssertDoesMeetConstraints
#2      RenderBox.size=.<anonymous closure>
#3      RenderBox.size=
#4      RenderCustomMultiChildLayoutBox.performLayout
#5      RenderObject.layout
#6      RenderBox.layout
#7      RenderProxyBoxMixin.performLayout
#8      RenderObject.layout
#9      RenderBox.layout
#10     RenderProxyBoxMixin.performLayout
#11     _RenderCustomClip.performLayout
#12     RenderObject.layout
#13     RenderBox.layout
#14     _RenderSingleChildViewport.performLayout
#15     RenderObject.layout
#16     RenderBox.layout
#17     RenderProxyBoxMixin.performLayout
#18     RenderObject.layout
#19     RenderBox.layout
#20     RenderProxyBoxMixin.performLayout
#21     RenderObject.layout
#22     RenderBox.layout
#23     RenderProxyBoxMixin.performLayout
#24     RenderObject.layout
#25     RenderBox.layout
#26     RenderProxyBoxMixin.performLayout
#27     RenderObject.layout
#28     RenderBox.layout
#29     RenderProxyBoxMixin.performLayout
#30     RenderObject.layout
#31     RenderBox.layout
#32     RenderProxyBoxMixin.performLayout
#33     RenderObject.layout
#34     RenderBox.layout
#35     RenderProxyBoxMixin.performLayout
#36     RenderObject.layout
#37     RenderBox.layout
#38     RenderProxyBoxMixin.performLayout
#39     RenderCustomPaint.performLayout
#40     RenderObject.layout
#41     RenderBox.layout
#42     RenderProxyBoxMixin.performLayout
#43     RenderObject.layout
#44     RenderBox.layout
#45     MultiChildLayoutDelegate.layoutChild
#46     _ScaffoldLayout.performLayout
#47     MultiChildLayoutDelegate._callPerformLayout
#48     RenderCustomMultiChildLayoutBox.performLayout
#49     RenderObject._layoutWithoutResize
#50     PipelineOwner.flushLayout
#51     RendererBinding.drawFrame
#52     WidgetsBinding.drawFrame
#53     RendererBinding._handlePersistentFrameCallback
#54     SchedulerBinding._invokeFrameCallback
#55     SchedulerBinding.handleDrawFrame
#56     SchedulerBinding._handleDrawFrame
#57     _invoke (dart:ui/hooks.dart:145:13)
#58     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#59     _drawFrame (dart:ui/hooks.dart:112:31)
The following RenderObject was being processed when the exception was fired: RenderCustomMultiChildLayoutBox#3c1d3 relayoutBoundary=up13 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
RenderObject: RenderCustomMultiChildLayoutBox#3c1d3 relayoutBoundary=up13 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
    parentData: <none> (can use size)
    constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=Infinity)
    size: Size(392.7, Infinity)
    child 1: RenderFlex#6f952 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body
        constraints: MISSING
        size: MISSING
        direction: vertical
        mainAxisAlignment: start
        mainAxisSize: max
        crossAxisAlignment: center
        verticalDirection: down
        child 1: RenderPositionedBox#bb9d9 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: offset=Offset(0.0, 0.0); flex=null; fit=null
            constraints: MISSING
            size: MISSING
            alignment: Alignment.center
            textDirection: ltr
            widthFactor: expand
            heightFactor: expand
            child: RenderSemanticsAnnotations#ca31f NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                parentData: offset=Offset(0.0, 0.0)
                constraints: MISSING
                size: MISSING
                child: RenderConstrainedBox#93da4 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
                    parentData: <none>
                    constraints: MISSING
                    size: MISSING
                    additionalConstraints: BoxConstraints(36.0<=w<=Infinity, 36.0<=h<=Infinity)
    child 2: RenderStack#cea44 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
        parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.floatingActionButton
        constraints: MISSING
        size: MISSING
        alignment: Alignment.centerRight
        textDirection: ltr
        fit: loose
        child 1: RenderTransform#c5da0 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
            parentData: not positioned; offset=Offset(0.0, 0.0)
            constraints: MISSING
            size: MISSING
            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: Alignment.center
            textDirection: ltr
            transformHitTests: true
            child: RenderTransform#42b70 NEEDS-LAYOUT NEEDS-PAINT
                parentData: <none>
                constraints: MISSING
                size: MISSING
                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: Alignment.center
                textDirection: ltr
                transformHitTests: true
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
_RenderInkFeatures object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
RenderPhysicalModel object was given an infinite size during layout.
The relevant error-causing widget was
Scaffold
════════════════════════════════════════════════════════════════════════════════
W/DynamiteModule(24486): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/elivery_app_ne(24486): Background concurrent copying GC freed 65713(2MB) AllocSpace objects, 5(228KB) LOS objects, 49% free, 3MB/6MB, paused 11.556ms total 412.304ms
I/DynamiteModule(24486): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(24486): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.


Solution

  • Wrap the ListView.builder with an Expanded widget. This will make the ListView take up as much space as available, with no overflowing.

    Also, when at all possible, try not to use shrinkWrap: true. it is terrible for performance, because it has to calculate the size of the scroll view every time the scroll position changes.