更新firebase URL字符串引发错误

yzuktlbb  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(123)

当我更改字符串URL字段时,我无法更新文档,98%的时间我都会遇到此错误。


这里是我的提升按钮代码:

ElevatedButton(
    onPressed: isProcessing
        ? null
        : () async {
            final action = await AlertDialogs.yesCancelDialog(
                context,
                ' بيانات الطفل ',
                'هل أنت متأكد من تعديل بيانات الطفل؟');
            if (!mounted) return;
            if (action == DialogsAction.yes) {
              setState(() => tappedYes = true);
              if (!mounted) return;
              //if yes
              if (_formKey.currentState!.validate()) {
                setState(() {
                  isLoading = true;
                  isProcessing = true;
                });
                Future.delayed(Duration(seconds: 12), () {
                  try {
                    final docChild = FirebaseFirestore.instance
                        .collection('users')
                        .doc(uid)
                        .collection('children')
                        .doc(widget.childID);
                    //update child info
                    docChild.update({
                      'image':
                          imgURL.isEmpty ? childImage : imgURL,
                      'name': childName.text,
                      'gender': selectedGender,
                      'height': int.parse(childHeight.text),
                      'birthday': DateTime.parse(birthday.text)
                    });
                  } on FirebaseException catch (e) {
                    // Caught an exception from Firebase.
                    print(
                        "Failed with error '${e.code}': ${e.message}");
                  }
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => NavPage()),
                  );
                  setState(() {
                    isLoading = false;
                    isProcessing = false;
                  });
                });
              }
            } else {
              setState(() => tappedYes = false);
              if (!mounted) return;
            }
          },
    style: ButtonStyle(
      backgroundColor:
          MaterialStateProperty.all(const Color(0xFF429EB2)),
    ),
    child: isLoading
        ? CircularProgressIndicator(
            color: Colors.white,
          )
        : const Text('حفظ التعديلات'))

然后它就像这样粘在负载上了

我尝试打印imgURL和childImg的内容,但两者都具有有效的字符串URL。

pxy2qtax

pxy2qtax1#

isLoading = false & isProcessing = false应该在更新方法之后。并且您应该在继续之前await更新。

Future.delayed(Duration(seconds: 12), () {
                                  try {
                                    final docChild = FirebaseFirestore.instance
                                        .collection('users')
                                        .doc(uid)
                                        .collection('children')
                                        .doc(widget.childID);
                                    //update child info
                                  await  docChild.update({
                                      'image':
                                          imgURL.isEmpty ? childImage : imgURL,
                                      'name': childName.text,
                                      'gender': selectedGender,
                                      'height': int.parse(childHeight.text),
                                      'birthday': DateTime.parse(birthday.text)
                                    }).then((_){
                                    setState(() {
                                    isLoading = false;
                                    isProcessing = false;
                                  });
                                    });
                                  } on FirebaseException catch (e) {
                                    // Caught an exception from Firebase.
                                    print(
                                        "Failed with error '${e.code}': ${e.message}");
                                  }
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => NavPage()),
                                  );
                                });
                              }

相关问题