Skip to content

How do i fetching List of Taxi Drivers on User flutter #970

@jacklyn01

Description

@jacklyn01

Hello Everyone,

I've been having error on my uber clone app on getDrivers(). the error is pointing to the DriverService and DriverModel.

E/flutter (21627): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter (21627): Receiver: null
E/flutter (21627): Tried calling:
E/flutter (21627): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter (21627): #1      new DriverModel.fromSnapshot (package:flutter_artisan_user/models/driver.dart:59:39)
E/flutter (21627): #2      DriverService.getDrivers.. (package:flutter_artisan_user/services/drivers.dart:10:43)
E/flutter (21627): #3      MappedListIterable.elementAt (dart:_internal/iterable.dart:411:31)
E/flutter (21627): #4      ListIterator.moveNext (dart:_internal/iterable.dart:340:26)
E/flutter (21627): #5      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
E/flutter (21627): #6      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
E/flutter (21627): #7      new List.of (dart:core-patch/array_patch.dart:50:28)
E/flutter (21627): #8      ListIterable.toList (dart:_internal/iterable.dart:211:44)
E/flutter (21627): #9      DriverService.getDrivers. (package:flutter_artisan_user/services/drivers.dart:10:60)
E/flutter (21627): #10     _MapStream._handleData (dart:async/stream_pipe.dart:213:31)
E/flutter (21627): #11     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
E/flutter (21627): #12     _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (21627): #13     _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (21627): #14     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (21627): #15     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter (21627): #16     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (21627): #17     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
E/flutter (21627): #18     _MapStream._handleData (dart:async/stream_pipe.dart:218:10)
E/flutter (21627): #19     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
E/flutter (21627): #20     _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (21627): #21     _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (21627): #22     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (21627): #23     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter (21627): #24     _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (21627): #25     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
E/flutter (21627): #26     _PendingEvents.schedule. (dart:async/stream_impl.dart:663:7)
E/flutter (21627): #27     _rootRun (dart:async/zone.dart:1346:47)
E/flutter (21627): #28     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (21627): #29     _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
E/flutter (21627): #30     _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23)
E/flutter (21627): #31     _rootRun (dart:async/zone.dart:1354:13)
E/flutter (21627): #32     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (21627): #33     _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
E/flutter (21627): #34     _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23)
E/flutter (21627): #35     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (21627): #36     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
E/flutter (21627):

DriverModel
    DriverModel.fromSnapshot(DocumentSnapshot snapshot)
{
    _name = snapshot.data()[NAME];
    _id = snapshot.data()[ID];
    _occupation = snapshot.data()[OCCUPATION];
    _specialization = snapshot.data()[SPECIALIZATION];
    _photo = snapshot.data()[PHOTO];
    _phone = snapshot.data()[PHONE];
    _rating = snapshot.data()[RATING];
    _votes = snapshot.data()[VOTES];
    _position = DriverPosition
    (
        lat: snapshot.data()[POSITION][LATITUDE],
        lng: snapshot.data()[POSITION][LONGITUDE],
        heading: snapshot.data()[POSITION][HEADING]
    );
}
    LatLng getPosition() 
    {
        return LatLng(_position.lat, _position.lng);
    }
}
class DriverPosition 
{
    final double lat;
    final double lng;
    final double heading;
  
    DriverPosition({this.lat, this.lng, this.heading});
}

DriverService

class DriverService 
{
    String collection = 'drivers';
    Stream<List<DriverModel>> getDrivers()
    {
        return firebaseFiretore.collection(collection).snapshots().map((event) =>
        event.docs.map((e) => DriverModel.fromSnapshot(e)).toList());
    }
    Future<DriverModel> getDriverById(String id) =>
    firebaseFiretore.collection(collection).doc(id).get().then((doc) 
    {
        return DriverModel.fromSnapshot(doc);
    });
    Stream<QuerySnapshot> driverStream() 
    {
        CollectionReference reference = FirebaseFirestore.instance.collection(collection);
        return reference.snapshots();
    }
}

Kindly assist please, I've been on this for weeks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions