cloud_firestore バージョンアップ エラー

cloud_firestore バージョンアップ による  class DocumentSnapshot が  class DocumentSnapshot<T extends Object?> での 変更による影響で前のコード修正

ここでの対応 – DocumentSnapshot

下のように  DocumentSnapshot を DocumentSnapshot<Map<String, dynamic>> へ 変更して 使用します . バージョンアップ されたのは レンプレートを使用しての Class ですので  Object として Map<String, dynamic> を付けて対応しました。

void modification() async{    
    
    //変更前 ---------------------------------------------------
    //late DocumentSnapshot snapshot;

    // 変更後 --------------------------------------------
    late DocumentSnapshot<Map<String, dynamic>> snapshot;
    
    try {
      snapshot = await FirebaseFirestore.instance
          .collection('UserColltion')
          .doc('WXDNlJkNVIHVgwWGet20') 
          .get();
    } catch (error) {     
      setState(() {});
      return;
    }
    final record = AdvertisingCollectionClass.fromSnapshot(snapshot);     
    print("-modification()--userphotoUrl-: ${record.photoUrl}");
    print("--modification()--userName--: ${record.transferUrl}");
   
  }

コード内で使用された AdvertisingCollectionClass


import 'package:cloud_firestore/cloud_firestore.dart';

class AdvertisingCollectionClass {


  String photoUrl = 'nonphotoUrl';

  String transferUrl = 'https://mogusatech.com/';

  DocumentReference? reference;
  AdvertisingCollectionClass.fromMap(Map<String, dynamic>? map, {this.reference})
  {

    if(map?['photoUrl'] == null){
          photoUrl = 'nonphotoUrl';
    }else{
      photoUrl = map?['photoUrl'];
    }
    if(map?['transferUrl'] == null){
     
      transferUrl = 'https://mogusatech.com/';
      //transferUrl = '';
    }else{
      transferUrl = map?['transferUrl'];
    }
  }  // fromMap  end


  //DocumentSnapshot snapshot  から
  //DocumentSnapshot<Map<String, dynamic>> snapshot  への変更
  AdvertisingCollectionClass.fromSnapshot(DocumentSnapshot<Map<String, dynamic>> snapshot)
      : this.fromMap(snapshot.data(), reference: snapshot.reference);
  @override

  String toString() => "Record< photoUrl:${photoUrl} , transferUrl:${transferUrl}>";

}  //  AdvertisingCollectionClass  end

cloud_firestore: ^3.1.13 での DocumentSnapshot Class

// Copyright 2020, the Chromium project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of cloud_firestore;

typedef FromFirestore<T> = T Function(
  DocumentSnapshot<Map<String, dynamic>> snapshot,
  SnapshotOptions? options,
);
typedef ToFirestore<T> = Map<String, Object?> Function(
  T value,
  SetOptions? options,
);

/// Options that configure how data is retrieved from a DocumentSnapshot
/// (e.g. the desired behavior for server timestamps that have not yet been set to their final value).
///
/// Currently unsupported by FlutterFire, but exposed to avoid breaking changes
/// in the future once this class is supported.
@sealed
class SnapshotOptions {}

/// A [DocumentSnapshot] contains data read from a document in your [FirebaseFirestore]
/// database.
///
/// The data can be extracted with the data property or by using subscript
/// syntax to access a specific field.
@sealed
abstract class DocumentSnapshot<T extends Object?> {
  /// This document's given ID for this snapshot.
  String get id;

  /// Returns the reference of this snapshot.
  DocumentReference<T> get reference;

  /// Metadata about this document concerning its source and if it has local
  /// modifications.
  SnapshotMetadata get metadata;

  /// Returns `true` if the document exists.
  bool get exists;

  /// Contains all the data of this document snapshot.
  T? data();

  /// {@template firestore.documentsnapshot.get}
  /// Gets a nested field by [String] or [FieldPath] from this [DocumentSnapshot].
  ///
  /// Data can be accessed by providing a dot-notated path or [FieldPath]
  /// which recursively finds the specified data. If no data could be found
  /// at the specified path, a [StateError] will be thrown.
  /// {@endtemplate}
  dynamic get(Object field);

  /// {@macro firestore.documentsnapshot.get}
  dynamic operator [](Object field);
}

class _JsonDocumentSnapshot implements DocumentSnapshot<Map<String, dynamic>> {
  _JsonDocumentSnapshot(this._firestore, this._delegate) {
    DocumentSnapshotPlatform.verifyExtends(_delegate);
  }

  final FirebaseFirestore _firestore;
  final DocumentSnapshotPlatform _delegate;

  @override
  String get id => _delegate.id;

  @override
  late final DocumentReference<Map<String, dynamic>> reference =
      _firestore.doc(_delegate.reference.path);

  @override
  late final SnapshotMetadata metadata = SnapshotMetadata._(_delegate.metadata);

  @override
  bool get exists => _delegate.exists;

  @override
  Map<String, dynamic>? data() {
    // TODO(rrousselGit): can we cache the result, to avoid deserializing it on every read?
    return _CodecUtility.replaceDelegatesWithValueInMap(
      _delegate.data(),
      _firestore,
    );
  }

  @override
  dynamic get(Object field) {
    return _CodecUtility.valueDecode(_delegate.get(field), _firestore);
  }

  @override
  dynamic operator [](Object field) => get(field);
}

/// A [DocumentSnapshot] contains data read from a document in your [FirebaseFirestore]
/// database.
///
/// The data can be extracted with the data property or by using subscript
/// syntax to access a specific field.
class _WithConverterDocumentSnapshot<T> implements DocumentSnapshot<T> {
  _WithConverterDocumentSnapshot(
    this._originalDocumentSnapshot,
    this._fromFirestore,
    this._toFirestore,
  );

  final DocumentSnapshot<Map<String, dynamic>> _originalDocumentSnapshot;
  final FromFirestore<T> _fromFirestore;
  final ToFirestore<T> _toFirestore;

  @override
  T? data() {
    if (!_originalDocumentSnapshot.exists) return null;

    return _fromFirestore(_originalDocumentSnapshot, null);
  }

  @override
  bool get exists => _originalDocumentSnapshot.exists;

  @override
  String get id => _originalDocumentSnapshot.id;

  @override
  SnapshotMetadata get metadata => _originalDocumentSnapshot.metadata;

  @override
  DocumentReference<T> get reference => _WithConverterDocumentReference<T>(
        _originalDocumentSnapshot.reference,
        _fromFirestore,
        _toFirestore,
      );

  @override
  dynamic get(Object field) => _originalDocumentSnapshot.get(field);

  @override
  dynamic operator [](Object field) => _originalDocumentSnapshot[field];
}

cloud_firestore: ^1.0.1 での DocumentSnapshot Class

// Copyright 2020, the Chromium project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of cloud_firestore;

/// A [DocumentSnapshot] contains data read from a document in your [FirebaseFirestore]
/// database.
///
/// The data can be extracted with the data property or by using subscript
/// syntax to access a specific field.

// 이전 버전에서는 템플렛을 사용하지 않았음
class DocumentSnapshot {
  final FirebaseFirestore _firestore;
  final DocumentSnapshotPlatform _delegate;

  DocumentSnapshot._(this._firestore, this._delegate) {
    DocumentSnapshotPlatform.verifyExtends(_delegate);
  }

  /// This document's given ID for this snapshot.
  String get id => _delegate.id;

  /// Returns the [DocumentReference] of this snapshot.
  DocumentReference get reference => _firestore.doc(_delegate.reference.path);

  /// Metadata about this [DocumentSnapshot] concerning its source and if it has local
  /// modifications.
  SnapshotMetadata get metadata => SnapshotMetadata._(_delegate.metadata);

  /// Returns `true` if the [DocumentSnapshot] exists.
  bool get exists => _delegate.exists;

  /// Contains all the data of this [DocumentSnapshot].
  Map<String, dynamic>? data() {
    return _CodecUtility.replaceDelegatesWithValueInMap(
        _delegate.data(), _firestore);
  }

  /// Gets a nested field by [String] or [FieldPath] from this [DocumentSnapshot].
  ///
  /// Data can be accessed by providing a dot-notated path or [FieldPath]
  /// which recursively finds the specified data. If no data could be found
  /// at the specified path, a [StateError] will be thrown.
  dynamic get(dynamic field) =>
      _CodecUtility.valueDecode(_delegate.get(field), _firestore);

  /// Gets a nested field by [String] or [FieldPath] from this [DocumentSnapshot].
  ///
  /// Data can be accessed by providing a dot-notated path or [FieldPath]
  /// which recursively finds the specified data. If no data could be found
  /// at the specified path, a [StateError] will be thrown.
  dynamic operator [](dynamic field) => get(field);
}

QuerySnapshot にても 同じように

コメント

PAGE TOP