Flutter -当我添加import 'package:path/path.dart'时出错

9o685dep  于 2023-05-23  发布在  Flutter
关注(0)|答案(3)|浏览(321)

我尝试使用图像路径上传图像,在我的应用程序正常之前。当我尝试添加import 'package:path / path.dart';和basename(image.path),为了上传我的图像,我在对话框警报中得到了一个新的错误。更多详细信息:这是我的代码:

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;
        });
      }

下面的代码,我得到了一个错误:

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()));
              },
            ),
          ],
        );
      },
    );
  }
xmd2e60i

xmd2e60i1#

dart覆盖框架的全局Context变量。:-(所以直到路径包开发人员修复这个问题,Günter Zöchbauer在注解中的解决方案是有效的。使用包的别名并通过别名访问它。如果你像我一样有其他变量叫做“path”,你需要重命名它们,例如:
导入“package:path/path.dart”作为路径;
然后例如使用

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

而不是

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

tmb3ates2#

上面的每一个答案都是正确的。我也想回答这个问题的细节。我遇到了同样的问题,你们都帮助了我。谢谢。

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

    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(path.basename(_image.path));
          final StorageUploadTask task = firebaseStorageRef.putFile(_image);
          return task;
        });
      }
dfuffjeb

dfuffjeb3#

使用以下Future<void> alertSuccess() async { // your code }

相关问题