简体   繁体   English

flutter - 无法调用另一个文件中的方法

[英]flutter - can't call a method in another file

I have create a dart file with the method below.我用下面的方法创建了一个 dart 文件。 As you can see its job is to delete a file in Firestore.如您所见,它的工作是删除 Firestore 中的文件。 What I do not understand is why I can't call it from an other dart file.我不明白的是为什么我不能从其他 dart 文件中调用它。 In the view where I want to use that I have imported 'import 'package:xxxxx/Services/Firestore/api_delete_file_in_firestore.dart';'在我想使用的视图中,我已经导入了 'import 'package:xxxxx/Services/Firestore/api_delete_file_in_firestore.dart';'

I am getting this error message: The method '_deleteFileInFireStore' isn't defined for the type '_Reference_Detail_Edit_SaveState'.我收到此错误消息:没有为类型“_Reference_Detail_Edit_SaveState”定义方法“_deleteFileInFireStore”。

Thank you for your help.谢谢您的帮助。


import 'package:firebase_storage/firebase_storage.dart';

class FireStore_Api{

  static void _deleteFileInFireStore(String fileToDelete) async {
    try {
      Reference firebaseStorageRef =
      FirebaseStorage.instance.ref().child(fileToDelete);
      await firebaseStorageRef.delete();
      print('Successfully deleted $fileToDelete storage item' );
    } catch (e) {
      return null;
    }
  }
}

now, this is where I am calling this function.现在,这就是我称之为 function 的地方。 I do not understand what is the problem.我不明白是什么问题。

 Padding(

                    padding: const EdgeInsets.only(right:25.0),
                    child: IconButton(onPressed: (){
                      //delete a record and the card displaying this record
                      // Delete the selected file

                      setState(() {
                        if (task_Attachments.length !=0) {
                          print ('delete' + result);

                          _deleteFileInFireStore(result); //delete the file selected in firestore
                          
                          task_Attachments.removeAt(j);
                          _delete(context);
                          
                        };
                      });
                    },
                      icon:Icon (Icons.delete, color: Colors.red,),),
                  ):

What was wrong with your function, the function you created is private , and private functions can only be accessed within the file in which it was created .您的 function 出了什么问题,您创建的 function 是私有的,私有函数只能在创建它的文件中访问

Remove the _ underscore from the function: _deleteFileInFireStore(String fileToDelete)从 function 中删除_下划线: _deleteFileInFireStore(String fileToDelete)

Final result最后结果

deleteFileInFireStore(String fileToDelete) deleteFileInFireStore(String fileToDelete)

In the file you want to access, you can first create an object for this class and try to access it through that object.在您要访问的文件中,您可以先为此 class 创建一个 object,然后尝试通过该 object 访问它。 (I know you define static but I don't understand what's the problem and this will help for solution) (我知道你定义了 static 但我不明白有什么问题,这将有助于解决)

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

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