使用颤振从firestore中获取数据?

时间:2021-07-26 15:30:08

标签: firebase flutter

我正在尝试从 GitHub 编译一个示例来协助我的项目。我正在使用 flutter 和 Firebase 开发电子商务应用程序。但我正在经历这个:

The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'.dart(argument_type_not_assignable)
 Object? json

utils.dart :

import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';

  class Utils {
  static StreamTransformer transformer<T>(
      T Function(Map<String, dynamic> json) fromJson) =>
  StreamTransformer<QuerySnapshot, List<T>>.fromHandlers(
    handleData: (QuerySnapshot data, EventSink<List<T>> sink) {
      final snaps = data.docs.map((doc) => doc.data()).toList();
      final products = snaps.map((json) => fromJson(json)).toList(); //// >> This is where the error appears

      sink.add(products);
    },
  );

 static DateTime? toDateTime(Timestamp value) {
// ignore: unnecessary_null_comparison
if (value == null) return null;

return value.toDate();
  }

 static dynamic fromDateTimeToJson(DateTime date) {
  // ignore: unnecessary_null_comparison
  if (date == null) return null;

   return date.toUtc();
  }
 }

这里出现另一个错误:

The argument type 'StreamTransformer<dynamic, dynamic>' can't be assigned to the parameter type 'StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List<FoodModel>>'.

FirebaseApi.dart :

 static Stream<List<FoodModel>> getprincipalproducts() =>
  FirebaseFirestore.instance
      .collection('PrincipalProducts')
      .snapshots()
      .transform(
        Utils.transformer(FoodModel.fromJson),//// >> This is where the error appears
      );

FoodModel.dart :

class FoodModel {
final String uid;
final String name;
final String description;
final String imageUrl;
final double rate;

 FoodModel({
required this.uid,
required this.name,
required this.description,
required this.imageUrl,
required this.rate,
 });
 FoodModel copyWith({
String? uid,
String? name,
String? description,
String? imageUrl,
double? rate,
  }) =>
  FoodModel(
    uid: uid ?? this.uid,
    name: name ?? this.name,
    description: description ?? this.description,
    imageUrl: imageUrl ?? this.imageUrl,
    rate: rate ?? this.rate,
  );

static FoodModel fromJson(Map<String, dynamic> json) => FoodModel(
    uid: json['uid'],
    name: json['name'],
    description: json['description'],
    imageUrl: json['imageUrl'],
    rate: json['rate'],
  );

Map<String, dynamic> toJson() => {
    'uid': uid,
    'name': name,
    'lastname': description,
    'imageUrl': imageUrl,
    'rate': rate,
  };
  }
<块引用> <块引用>

任何帮助将不胜感激! 我使用的是最后一个版本的 flutter,所以这段代码在以前的版本上运行良好 ??

0 个答案:

没有答案