简体   繁体   English

Flutter firebase IOS设备上传照片问题

[英]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."我想从 flutter 中的图库上传图片。Android 设备工作正常但是当我单击 Ios 设备上的更改按钮时,我收到错误消息“引发了另一个异常:LateInitializationError:字段'indirmeBaglantisi'尚未初始化。 “

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

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.您可能忘记请求使用用户照片库的许可,因为这是您必须在 IOS 上做的事情。

Go to your IOS folder and inside of that go to the Runner folder. Go 到您的 IOS 文件夹,go 到 Runner 文件夹。 Find the info.plist file and add in the following:找到 info.plist 文件并添加以下内容:

<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.您可以将第二行的文本替换为您希望用户看到的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM