【问题标题】:DateTime not a subtype of type TimeStamp/Unhandled Exception: Invalid argument: Instance of 'Future<LocationData>DateTime 不是 TimeStamp/Unhandled 异常类型的子类型:无效参数:'Future<LocationData> 的实例
【发布时间】:2020-08-31 21:18:03
【问题描述】:

所以我正在使用附近的连接 API 来发现我周围的设备并将它们的数据存储在 Firestore 中,但是我不断收到 2 条关于我从与我联系的用户那里获得的位置以及我开始联系的时间的警告和他们一起

这是两个警告:

1)DateTime 不是 TimeStamp 类型的子类型

2) 未处理的异常:无效参数:Future<.locationdata.> 的实例 当我尝试将这些值添加到 firestore 时

这是我的发现方法:

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);
    }
  }

这是我从 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);
    });
  }

请问有什么解决办法吗?

【问题讨论】:

    标签: firebase flutter timestamp location google-nearby-connections


    【解决方案1】:

    使用异步函数将 Future<.locationdata.> 转换为 LocationData。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-18
      • 2020-06-14
      • 2020-07-08
      • 2019-11-10
      • 2023-03-05
      • 2021-01-12
      • 2020-11-19
      • 1970-01-01
      相关资源
      最近更新 更多