【问题标题】:How to get updateProfile() working in firebase?如何让 updateProfile() 在 Firebase 中工作?
【发布时间】:2020-06-25 13:32:03
【问题描述】:

我正在尝试使用 Firebase 创建应用。但是当我使用 updateProfile() 时,我得到了这个错误。没有为“Future”类型定义“updateProfile”方法。

Future  updateProfilePic(picUrl) {
    var userInfo = new UserUpdateInfo();
    userInfo.photoUrl = picUrl;
    final user = FirebaseAuth.instance.currentUser();
    user.updateProfile(userInfo).then((val) {
      FirebaseAuth.instance.currentUser().then((user) {
        Firestore.instance
            .collection('/Recycling Points')
            .where('uid', isEqualTo: user.uid)
            .getDocuments()
            .then((docs) {
          Firestore.instance
              .document('/Recycling Points/${docs.documents[0].documentID}')
              .updateData({'image': picUrl}).then((value){
                print ('uploaded');
          });
        });
      }).catchError((e) {
        print(e);
      });
    });
  }

编辑: 这是完整的代码。

导入包

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:smartbin/models/recycle.dart';
import 'package:smartbin/models/user.dart';

数据库服务

class DatabaseService {
  final String uid;
  DatabaseService({this.uid});

收藏参考

  final CollectionReference recyclepointsCollection =
      Firestore.instance.collection('Recycle Points');

  Future updateUserData(String name, String email, String image,
      int organicWaste, int nonOrganicWaste, int recycleWaste) async {
    return await recyclepointsCollection.document(uid).setData({
      'name': name,
      'email': email,
      'image': image,
      'Organic Waste ': organicWaste,
      'Non Organic Waste': nonOrganicWaste,
      'Recycling Waste ': recycleWaste,
    });
  }

从快照中回收点列表

  List<Recycle> _recycleListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.documents.map((doc) {
      return Recycle(
          name: doc.data['name'] ?? '',
          email: doc.data['email'] ?? '',
          image: doc.data['image'] ?? '',
          organicWaste: doc.data['organicWaste'] ?? 0,
          nonOrganicWaste: doc.data['nonOrganicWaste'] ?? 0,
          recycleWaste: doc.data['recycleWaste'] ?? 0);
    }).toList();
  }

更新个人资料图片代码

void  updateProfilePic(picUrl) {
    var userInfo = new UserUpdateInfo();
    userInfo.photoUrl = picUrl;
    final user = FirebaseAuth.instance.currentUser();
    user.updateProfile(userInfo).then((val) {
      FirebaseAuth.instance.currentUser().then((user) {
        Firestore.instance
            .collection('/Recycling Points')
            .where('uid', isEqualTo: user.uid)
            .getDocuments()
            .then((docs) {
          Firestore.instance
              .document('/Recycling Points/${docs.documents[0].documentID}')
              .updateData({'image': picUrl}).then((value){
            print ('uploaded');
          });
        });
      }).catchError((e) {
        print(e);
      });
    });
  }

快照中的用户数据

  UserData _userDataFromSnapshot(DocumentSnapshot snapshot) {
    return UserData(
        uid: uid,
        name: snapshot.data['name'],
        email: snapshot.data['email'],
        image: snapshot.data['image'],
        organicWaste: snapshot.data['organicWaste'],
        nonOrganicWaste: snapshot.data['nonOrganicWaste'],
        recycleWaste: snapshot.data['recycleWaste']);
  }

获取回收点流

  Stream<List<Recycle>> get recyclePoints {
    return recyclepointsCollection.snapshots().map(_recycleListFromSnapshot);
  }

获取用户文档流

  Stream<UserData> get userData {
    return recyclepointsCollection
        .document(uid)
        .snapshots()
        .map(_userDataFromSnapshot);
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    尝试这样做

    void updateProfilePic(picUrl) {
        var userInfo = new UserUpdateInfo();
        userInfo.photoUrl = picUrl;
        final user = FirebaseAuth.instance.currentUser();
        user.updateProfile(userInfo).then((val) {
          FirebaseAuth.instance.currentUser().then((user) {
            Firestore.instance
                .collection('/Recycling Points')
                .where('uid', isEqualTo: user.uid)
                .getDocuments()
                .then((docs) {
              Firestore.instance
                  .document('/Recycling Points/${docs.documents[0].documentID}')
                  .updateData({'image': picUrl}).then((value){
                    print ('uploaded');
              });
            });
          }).catchError((e) {
            print(e);
          });
        });
      }
    

    从 Future 更改为 void 方法。

    【讨论】:

    • 试过了。仍然出现此错误。 The method 'updateProfile' isn't defined for the type 'Future'.
    • 你能分享你如何使用'updateProfile'的完整代码
    • 如果错误仍然显示“未为类型 'Future' 定义方法 'updateProfile'。”将“未来”更改为“无效”之后。
    • 如果您进行了热重载;尝试重新启动应用程序
    • 我做到了。我仍然得到一个错误。 44:10: Error: The method 'updateProfile' isn't defined for the class 'Future&lt;FirebaseUser&gt;'. - 'Future' is from 'dart:async'. - 'FirebaseUser' is from 'package:firebase_auth/firebase_auth.dart' ('/C:/Users/Predator/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.16.1/lib/firebase_auth.dart'). Try correcting the name to the name of an existing method, or defining a method named 'updateProfile'. user.updateProfile(userInfo).then((val) {^^^^^^^^^^^^^
    【解决方案2】:

    请检查此代码是否解决了您的问题

    void updateProfilePic(picUrl) async {
            var userInfo = new UserUpdateInfo();
            userInfo.photoUrl = picUrl;
            final user = await FirebaseAuth.instance.currentUser();
            user.updateProfile(userInfo).then((val) {
              FirebaseAuth.instance.currentUser().then((user) {
                Firestore.instance
                    .collection('/Recycling Points')
                    .where('uid', isEqualTo: user.uid)
                    .getDocuments()
                    .then((docs) {
                  Firestore.instance
                      .document('/Recycling Points/${docs.documents[0].documentID}')
                      .updateData({'image': picUrl}).then((value){
                        print ('uploaded');
                  });
                });
              }).catchError((e) {
                print(e);
              });
            });
          }
    

    【讨论】:

    • 不,它不起作用。得到同样的错误。 Error: The method 'updateProfile' isn't defined for the class 'Future&lt;FirebaseUser&gt;'. - 'Future' is from 'dart:async'. - 'FirebaseUser' is from 'package:firebase_auth/firebase_auth.dart'
    猜你喜欢
    • 2020-02-17
    • 2020-08-27
    • 1970-01-01
    • 2016-11-28
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2017-03-14
    相关资源
    最近更新 更多