微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter Firebase,添加第二张照片但 firebase 只使用一个 url

如何解决Flutter Firebase,添加第二张照片但 firebase 只使用一个 url

我在我的个人资料表单注册中有此代码。它将拍摄两张不同的照片,但在提交表单后,firebase 只为它们使用一个 Url。这是在用户注册时启动的个人资料表单。这提供给 ProfileBloc,用户存储库是调用 firebase 的地方。它适用于一张照片,我可以在需要第一张照片时调用它。 Firebase 可以看到照片和宠物照片,但两者使用相同的网址,因此它只会显示第一张上传的照片。

代码

    class ProfileForm extends StatefulWidget {
      final UserRepository _userRepository;
    
      ProfileForm({@required UserRepository userRepository,userId})
          : assert(userRepository != null),_userRepository = userRepository;
    
      @override
      _ProfileFormState createState() => _ProfileFormState();
    }
   
    File photo,petPhoto;
     @override
      Widget build(BuildContext context) {
        Size size = MediaQuery.of(context).size;
    
    BlocBuilder<ProfileBloc,ProfileState>(
    builder: (context,state) {
    Container(
    width: size.width,child: Stack(
    children: <Widget>[
    CircleAvatar(
    radius: size.width * 0.3,backgroundColor: Colors.transparent,child: photo == null
    ? GestureDetector(
    onTap: () async {
    File getPic =
    await FilePicker.getFile(
    type: FileType.image);
    if (getPic != null) {
    setState(() {
    photo = getPic;
    });
    }
    },child: Image.asset(
    'assets/images/addPhoto.png'),)
        : GestureDetector(
    onTap: () async {
    File getPic =
    await FilePicker.getFile(
    type: FileType.image);
    if (getPic != null) {
    setState(() {
    photo = getPic;
    });
    }
    },child: CircleAvatar(
    radius: size.width * 0.3,backgroundImage: FileImage(photo),),Positioned(
    top: 50,left: 150.0,child: petPhoto == null
    ? GestureDetector(
    onTap: () async {
    File getPetPic =
    await FilePicker.getFile(
    type: FileType.image);
    if (getPetPic != null) {
    setState(() {
    petPhoto = getPetPic;
    });
    }
    },child: Image.asset(
    'assets/images/addPetPhoto.png'),)
        : GestureDetector(
    onTap: () async {
    File getPetPic =
    await FilePicker.getFile(
    type: FileType.image);
    if (getPetPic != null) {
    setState(() {
    petPhoto = getPetPic;
    });
    }
    },backgroundImage:
    FileImage(petPhoto),)
    ],));}}
    
    Future<User> userInfo(userId) async {
      User _user = User();
    
      await _firestore.collection('users').document(userId).get().then((user) {
        _user.uid = user.documentID;
        _user.photo = user['photoUrl'];
        _user.petPhoto = user['petPhotoUrl'];
      });
      return _user;
    }
    
    //profile setup
    Future<void> profileSetup(File photo,String userId,File petPhoto) async {
      StorageUploadTask storageUploadTask;
      storageUploadTask = FirebaseStorage.instance
          .ref()
          .child('userPhotos')
          .child(userId)
          .child(userId)
          .putFile(photo);
    
    
      return await storageUploadTask.onComplete.then((ref) async {
        await ref.ref.getDownloadURL().then((url) async {
          await ref.ref.getDownloadURL().then((urlPet) async {
            await _firestore.collection('users').document(userId).setData({
              'uid': userId,'photoUrl': url,'petPhotoUrl': urlPet,});
          });
        });
      });
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。