firebase 配置文件.dart上的_TypeError(类型“String”不是类型“int”(属于“index”)的子类型)

pu3pd22g  于 2023-02-13  发布在  其他
关注(0)|答案(4)|浏览(162)

我绑打电话给用户出现在个人资料页面的应用程序块时,我修复了它,我面临着其他问题,这是一个如果有任何解决方案可以帮助我

import 'package:flutter/foundation.dart';

class UserModel {
  String? uid;
  String? Username;
  String? email;
  String? photoUrl;
  

  UserModel(
      {this.uid, this.email, this.Username,  this.photoUrl});

  // receving data from the server
  factory UserModel.fromMap(Map) {
    return UserModel(
      uid: Map['userId'],
      Username: Map['Username'],
      email: Map['email'],
      photoUrl: Map['photoUrl'],
    );
  }

  // /// sending data to firestore
  Map<String, dynamic> toMap() {
    return {
      'userId': uid,
      'Username': Username,
      'email': email,
      'photoUrl': photoUrl,
    };
  }
}

错误图片

8dtrkrch

8dtrkrch1#

不要使用Map作为变量名,它是一个DataType

factory UserModel.fromMap(map) {
    return UserModel(
      uid: map['userId'],
      Username: map['Username'],
      email: map['email'],
      photoUrl: map['photoUrl'],
    );
  }
wrrgggsh

wrrgggsh2#

通过使用此:

// receving data from the server
  factory UserModel.fromMap(Map) {
    return UserModel(
      uid: Map['userId'],
      Username: Map['Username'],
      email: Map['email'],
      photoUrl: Map['photoUrl'],
    );
  }

你正在引用Map作为它的类型,所以它抛出错误.你需要引用一个Map对象,而不是Map类型,所以尝试这个:

// receving data from the server
  factory UserModel.fromMap(Map data) {
    return UserModel(
      uid: data['userId'],
      Username: data['Username'],
      email: data['email'],
      photoUrl: data['photoUrl'],
    );
  }
dz6r00yl

dz6r00yl3#

当你需要声明响应为Map〈String,dynamic〉的数据时,即使在这样做之后,如果错误仍然存在,尝试添加Map['userId].toString()||将['userId]Map为字符串。请参考以下代码

final favoriteResponse = await http.get(url);
  final favoriteData =
      json.decode(favoriteResponse.body) as Map<String, dynamic>?;
  final List<Product> loadedProducts = [];
  extractedData?.forEach((prodId, prodData) {
    loadedProducts.add(Product(
        id: prodId,
        title: prodData['title'],
        description: prodData['description'],
        price: prodData['price'],
        imageUrl: prodData['imageUrl'],
        isFavorite:
            favoriteData == null ? false : favoriteData[prodId] ?? false));
  });
  _items = loadedProducts;
  notifyListeners();
}
qeeaahzv

qeeaahzv4#

确保
用户ID
是你消防站里的一根绳子

相关问题