IOS 设备上的 Flutter firebase 上传照片问题

Flutter firebase upload photo problem on IOS devices

我想在 flutter 中上传图库中的图片。 Android 设备工作正常但是当我单击 Ios 设备上的更改按钮时,出现错误“抛出另一个异常:LateInitializationError:字段 'indirmeBaglantisi' 尚未初始化。”。 =12=]

什么可能导致此错误?这是我的代码:

class _AyarlarState extends State<Ayarlar> {

 final AuthService _auth = AuthService();
 FirebaseAuth auth = FirebaseAuth.instance;
 late String indirmeBaglantisi;
 late File yuklenecekDosya;
@override
 void initState(){
   super.initState();
   WidgetsBinding.instance!.addPostFrameCallback((_) => baglantiAl());
 }

 baglantiAl() async {
    String baglanti = await FirebaseStorage.instance
      .ref()
      .child("profilresimleri")
      .child(auth.currentUser!.uid)
      .child("profilResmi.png").getDownloadURL();

      setState(() {
        indirmeBaglantisi = baglanti;
      });
 }

 kameradanYukle() async {
     // ignore: deprecated_member_use
     var alinanDosya = await ImagePicker().getImage(source: ImageSource.gallery);
     setState(() {
       yuklenecekDosya = File(alinanDosya!.path);
     });
    Reference referansYol = FirebaseStorage.instance
      .ref()
      .child("profilresimleri")
      .child(auth.currentUser!.uid)
      .child("profilResmi.png");

    UploadTask yuklemeGorevi = referansYol.putFile(yuklenecekDosya);

    String url = await (await yuklemeGorevi).ref.getDownloadURL();
    
    setState(() {
      indirmeBaglantisi = url;
    });
     
 }

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
    
      body: Container(
        color: Colors.black,
        child: Stack(
        children: <Widget>[
            Container(
              decoration: BoxDecoration(
                color: Color(0XFF1E1E37),
              
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top:130),
              child: Column(
                children: <Widget> [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget> [
                      Container(

                        child: Row(
                          children: [
                            ClipOval(
                              
                              child: indirmeBaglantisi == null ? Image.asset("assets/images/Oval2.png") : Image.network(indirmeBaglantisi,height: 100,width:100 ,fit:BoxFit.fitWidth),
                              )
                          ],
                        ),
                         
                          ),
                    ],
                  ), 
                  SizedBox(height: 20),
              // ignore: deprecated_member_use
               RaisedButton.icon(
                  padding: EdgeInsets.symmetric(vertical: 8.0,horizontal: 10.0),
                  onPressed: () {

                   kameradanYukle(); 

                  },
                  shape:RoundedRectangleBorder(
                  
                  borderRadius: BorderRadius.all(Radius.circular(30)),
                ) ,
                  icon: Icon(Icons.insert_photo_outlined, color: Colors.blue,), label: Text('Profil Fotoğrafını Değiştir'),
                  color: Colors.white,
                  ),
                 

您可能忘记了请求许可才能使用用户的照片库,因为这是您必须在 IOS 上做的事情。

转到您的 IOS 文件夹,然后在其中转到 Runner 文件夹。找到 info.plist 文件并添加以下内容:

<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library?</string>

您可以将第二行的文本替换为您希望用户看到的内容。