颤振问题:"Timestamp"类型不是"DateTime"类型的子类型 [英] Flutter issue: type 'Timestamp' is not a subtype of type 'DateTime'

查看:106
本文介绍了颤振问题:"Timestamp"类型不是"DateTime"类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for some help for an app my team and I are working on. We are facing the following problem for weeks :

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

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

We want to get these user data from Cloud Firestore:

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

只有出生许可证在Cloud Firestore中存储为[Timestamp],因此我们将这些数据解析为[DateTime].

Only birth and license are stored as [Timestamp] in Cloud Firestore so we parse these data to [DateTime].

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

Here's our code to get user's data :

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

在这里我们调用这些函数,并在其中出现问题:

And here's where we call these functions and where the issue seems to appear :

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上一切正常.

The issue only occurs on iOS. Everything is OK on Android.

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

The problem should not come from the link between Cloud Firestore and the app because we can get other stored information.

推荐答案

在我的项目中,我使用

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

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

and works well, it's another option.

这篇关于颤振问题:"Timestamp"类型不是"DateTime"类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆