颤振问题:“时间戳”类型不是“ DateTime”类型的子类型

时间:2019-03-14 18:55:25

标签: firebase dart flutter timestamp google-cloud-firestore

我正在为我的团队正在开发的应用程序寻求帮助。数周以来,我们面临以下问题:

flutter type 'Timestamp' is not a subtype of type 'DateTime'

我们想从 Cloud Firestore 获取这些用户数据:

  • 出生[日期时间]
  • 性别[String]
  • 许可证[DateTime]
  • 名称[String]
  • 大小[String]
  • 权重[String]

只有出生许可证作为[时间戳记]存储在Cloud Firestore中,因此我们将这些数据解析为[DateTime]。

这是我们获取用户数据的代码:

import 'package:cloud_firestore/cloud_firestore.dart';

class User{
  final String _name;
  final String _gender; // 'homme' or 'femme' or 'autre'
  final DateTime _birth;
  final DateTime _license;
  final double _weight;
  final int _size;
  DocumentReference ref;

  User(this._birth,this._gender,this._license,this._name,this._size,this._weight,this.ref);

  User.fromMap(Map<String, dynamic> map, {this.ref})
      : assert(map['name'] != null && map['gender'] != null && map['birth'] != null && map['weight'] != null && map['size'] != null),
        _name = map['name'],
        _gender = map['gender'],
        _birth = map['birth'] as DateTime,
        _license = map['license'] as DateTime,
        _weight = double.parse(map['weight']),
        _size = int.parse(map['size']);

  User.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, ref: snapshot.reference);

  String get name => _name;
  String get gender => _gender;
  DateTime get birth => _birth;
  DateTime get license => _license;
  double get weight => _weight;
  int get size => _size;
}

在这里我们调用这些函数,并且似乎出现问题了:

import 'package:bappsalcolit/pages/homepage.dart';
import 'package:flutter/material.dart';
import 'package:bappsalcolit/sign_in/auth.dart';
import 'package:bappsalcolit/sign_in/log_page.dart';
import 'package:bappsalcolit/ads_display.dart';
import 'package:bappsalcolit/dbase/model/global_info.dart';

import 'package:cloud_firestore/cloud_firestore.dart';

User currentUser = User(null, null, null, null, null, null, null);

class RootPage extends StatefulWidget {
  RootPage({this.auth});

  final AuthImpl auth;

  @override
  State<StatefulWidget> createState() => new _RootPageState();
}

enum AuthStatus {
  NOT_DETERMINED,
  NOT_SIGNED_IN,
  SIGNED_IN,
}

class _RootPageState extends State<RootPage> {
  AuthStatus authStatus = AuthStatus.NOT_DETERMINED;
  String _userID = "";

  @override
  void initState(){
    super.initState();
    Ads.hideBanner();
    widget.auth.getCurrentUser().then((user) {
      setState(() {
        if(user != null) {
          _userID = user?.uid;
        }
        authStatus =
        user?.uid == null ? AuthStatus.NOT_SIGNED_IN : AuthStatus.SIGNED_IN;
      });
    });
  }

  void _signedIn() {
    widget.auth.getCurrentUser().then((user){
      setState(() {
        _userID = user?.uid.toString();
      });
    });
    setState(() {
      authStatus = AuthStatus.SIGNED_IN;
    });
  }

  void _signedOut() {
    setState(() {
      authStatus = AuthStatus.NOT_SIGNED_IN;
      _userID = "";
    });
  }

  Widget _buildWaitingScreen() {
    return Scaffold(
      body: Container(
        height: 20.0,
        width: 20.0,
        alignment: Alignment.center,
        child: CircularProgressIndicator(),
      ),
    );
  }

  //========== Here's where the problem seems to appear ==========\\
  gettingSnapshots(){
    Firestore db = Firestore.instance;
    DocumentSnapshot userDS;
    db.collection('users').document(_userID).snapshots().listen((ds) async{
      if (ds.exists) {
        userDS = await db.collection('users').document(_userID).get();
        try {
          currentUser = User.fromSnapshot(userDS);
        } catch (e) {
          print('Error 1131: $e');
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    switch (authStatus) {
      case AuthStatus.NOT_SIGNED_IN:
        return new SignIn(
          auth: widget.auth,
          onSignedIn: _signedIn,
        );
        break;
      case AuthStatus.SIGNED_IN:
        if(_userID.length > 0 && _userID != null) {
          gettingSnapshots();
          return new HomePage(
            userID: _userID,
            auth: widget.auth,
            onSignedOut: _signedOut,
          );
        }
        break;
      case AuthStatus.NOT_DETERMINED:
        return _buildWaitingScreen();
        break;
    }
    return _buildWaitingScreen();
  }
}

仅在iOS上发生此问题。在Android上一切正常。

问题不应该来自Cloud Firestore与应用程序之间的链接,因为我们可以获得其他存储的信息。

3 个答案:

答案 0 :(得分:0)

我们解决了这个问题。

我们将解析后的[DateTime]值存储在[String]中,而不是对接收的值进行操作。因此,我们以与上述相同的方式将这些参数解析回[DateTime]:

import 'package:cloud_firestore/cloud_firestore.dart';

class User{
  final String _name;
  final String _gender; // 'homme' or 'femme' or 'autre'
  final DateTime _birth;
  final DateTime _license;
  final double _weight;
  final int _size;
  DocumentReference ref;

  User(this._birth,this._gender,this._license,this._name,this._size,this._weight,this.ref);

  User.fromMap(Map<String, dynamic> map, {this.ref})
      : assert(map['name'] != null && map['gender'] != null && map['birth'] != null && map['weight'] != null && map['size'] != null),
        _name = map['name'],
        _gender = map['gender'],
        _birth = map['birth'] as DateTime,
        _license = map['license'] as DateTime,
        _weight = double.parse(map['weight']),
        _size = int.parse(map['size']);

  User.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, ref: snapshot.reference);

  String get name => _name;
  String get gender => _gender;
  DateTime get birth => _birth;
  DateTime get license => _license;
  double get weight => _weight;
  int get size => _size;
}

所以这个问题已经解决了。很高兴知道为什么以前的存储方式不好。

答案 1 :(得分:0)

在我的项目中,我使用

birth: parsedJson['birth'].toDate()

效果很好,这是另一种选择。

答案 2 :(得分:0)

我刚开始接触 flutter,不知何故,我对上面的答案有一种心理障碍。

然而,这是我发现对我有用的东西。

import 'package:cloud_firestore/cloud_firestore.dart';

class User{
  final String _name;
  final String _gender; // 'homme' or 'femme' or 'autre'
  final DateTime _birth;
  final DateTime _license;
  final double _weight;
  final int _size;
  DocumentReference ref;

  User(this._birth,this._gender,this._license,this._name,this._size,this._weight,this.ref);

  User.fromMap(Map<String, dynamic> map, {this.ref})
      : assert(map['name'] != null && map['gender'] != null && map['birth'] != null && map['weight'] != null && map['size'] != null),
        _name = map['name'],
        _gender = map['gender'],
        _birth = (map['birth'] as Timestamp).toDate(),
        _license =(map['license'] as Timestamp).toDate(),
        _weight = double.parse(map['weight']),
        _size = int.parse(map['size']);

  User.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, ref: snapshot.reference);

  String get name => _name;
  String get gender => _gender;
  DateTime get birth => _birth;
  DateTime get license => _license;
  double get weight => _weight;
  int get size => _size;
}