Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - I get error when i add import 'package:path/path.dart'

I tried to use the image path to upload images, before my application was fine. when I try to add import 'package: path / path.dart'; and basename (image.path) for the purpose of uploading my image, I get a new error in my dialog alert. more details in context: context. This is my code:

import 'package:path/path.dart';

    Future uploadFile() async {
        final FirebaseUser user = await FirebaseAuth.instance.currentUser();
        final userId = user.uid;
        _imageList.forEach((_image) {
          final StorageReference firebaseStorageRef = FirebaseStorage.instance
              .ref()
              .child('category')
              .child('food')
              .child('images')
              .child(username)
              .child(basename(_image.path));
          final StorageUploadTask task = firebaseStorageRef.putFile(_image);
          return task;
        });
      }

and following code where I get an error:

Future<void> alertSuccess() async {
    return showDialog<void>(

    /*this error*/
    context: context,
    /*------------*/

      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Success'),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text(
                    'You success save data.'),
              ],
            ),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text('OK'),
              onPressed: () {
                Navigator.pop(context);
                Navigator.pushReplacement(
                    context,
                    new MaterialPageRoute(
                        builder: (context) => PublicService()));
              },
            ),
          ],
        );
      },
    );
  } 
like image 691
UnderdoX Avatar asked Mar 04 '23 15:03

UnderdoX


1 Answers

path.dart overwrites the global Context variable of the framework. :-( So until the path-package developers fix this, the solution from Günter Zöchbauer in the comments is valid. Use an alias for the package and access it through the alias. If you have, like me, other variables called "path", you need to rename them, so e.g.:

import 'package:path/path.dart' as path;

then e.g. use

final imgPath = path.join(
  (await _localPath),
  '${DateTime.now()}.png',
);

instead of

final path = join(
  (await _localPath),
  '${DateTime.now()}.png',
);
like image 54
Southbay Avatar answered Mar 20 '23 07:03

Southbay