1

So I am using the nearby connections API to discover devices around me and store their data in firestore however I keep getting 2 warnings about the location I am getting from the user that I came in contact with and the time i came in contact with them

These are the 2 warnings:

1)DateTime not a subtype of type TimeStamp

2)Unhandled Exception: Invalid argument: Instance of Future<.LocationData.> as I try to add these values to firestore

here is my discovery method:

void discovery() async {
    try {
      bool a = await Nearby().startDiscovery(loggedInUser.email, strategy,
          onEndpointFound: (id, name, serviceId) async {
        print('I saw id:$id with name:$name'); // the name here is an email

        var docRef =
            _firestore.collection('users').document(loggedInUser.email);

        //  When I discover someone I will see their email
        docRef.collection('met_with').document(name).setData({
          'email': await getUsernameOfEmail(email: name),
          'contact time': DateTime.now() as Timestamp ,
          'contact location': location.getLocation(),
        });
      }, onEndpointLost: (id) {
        print(id);
      });
      print('DISCOVERING: ${a.toString()}');                                                      
    } catch (e) {
      print(e);
    }
  }

This is another method where I retrieve the info I discovered from firestore:

void addContactsToList() async {
    await getCurrentUser();
    _firestore
        .collection('users')
        .document(loggedInUser.email)
        .collection('met_with')
        .snapshots()
        .listen((snapshot) {

   for (var doc in snapshot.documents) {
        String currEmail = doc.data['email'];
        DateTime currTime = doc.data.containsKey('contact time')
            ? (doc.data['contact time'] as Timestamp).toDate()
            : null;
        String currLocation = doc.data.containsKey('contact location')
            ? doc.data['contact location']
            : null;
        String _infection = doc.data['infected'];
        if (!contactTraces.contains(currEmail)) {
          contactTraces.add(currEmail);
          contactTimes.add(currTime);
          contactLocations.add(currLocation);
          infection.add(_infection);
        }
      }
      setState(() {});
      print(loggedInUser.email);
    });
  }

Any fix for this please?

Omar Hajj
  • 41
  • 2
  • 5

1 Answers1

1

Use an async function to convert the Future<.LocationData.> to LocationData.

     var data;
 void convertData() async{
 var futuredata = await FutureLocationData;

 setState(() {
  data = futuredata   });
 }