Ad

Flutter Firebase Upload Photo Problem On IOS Devices

I want to upload an image from the gallery in flutter. Android devices are worikng perfectly However when I click on the change button on Ios devices, I get an error "Another exception was thrown: LateInitializationError: Field 'indirmeBaglantisi' has not been initialized."

What could be causing this error? Here is my code:

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,
                  ),
                 
Ad

Answer

It's likely that you forgot to ask for permission to use the user's photo library as that is something you must do on IOS.

Go to your IOS folder and inside of that go to the Runner folder. Find the info.plist file and add in the following:

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

You can replace the second line's text with whatever you want the user to see.

Ad
source: stackoverflow.com
Ad