Local variable 'basename' can't be referenced before it is declared / Flutter

Viewed 2179

I'm having an issue with base name as I look to official documentation of basename and everything seems to me "ok". But it still shows errors on basename. Thank you for your time!

Errors Error with basename: The expression doesn't evaluate to a function, so it can't be invoked Local variable 'basename' can't be referenced before it is declared.

Code Snippet

 Future uploadImageToFirebase(BuildContext context) async {

    String basename = basename(_imageFile.path);
    StorageReference firebaseStorageRef =
        FirebaseStorage.instance.ref().child('uploads/$basename');
    StorageUploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    taskSnapshot.ref.getDownloadURL().then(
          (value) => print("Done: $value"),
        );
  }

context part code snippet

  void addToFb() async {
    final User user = FirebaseAuth.instance.currentUser;
    ...

    dbRef.add({
    ...
    }).then((res) {
      isLoading = false;

      Navigator.pushReplacement(
        context,             <--- HERE I GOT AND ERROR
        MaterialPageRoute(builder: (context) => Home(uid: widget.uid)),
      );
    }).catchError(
  (err) {
    showDialog(
      context: context,      <--- HERE I GOT AND ERROR
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Error"),
          content: Text(err.message),
          actions: [
          ...
          ],
        );
      },
...
}
2 Answers

String basename = basename(_imageFile.path);

You're using the same name. Change the variable's name to something else, e.g. String _basename = basename(_imageFile.path);

What you're doing is basically "Set a value to variable 'basename' by calling 'basename' function". Can you see the name clash?

if dart constructor not create , you get some error message:

Local variable 'x' can't be referenced before it is declared / Flutter
Related