在我的应用程序正常之前,我尝试使用图像路径上传图像。当我尝试添加 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()));
},
),
],
);
},
);
}
最佳答案
path.dart 覆盖框架的全局 Context 变量。
:-(
因此,在路径包开发人员解决此问题之前,Günter Zöchbauer 在评论中的解决方案是有效的。
使用包的别名并通过别名访问它。
如果您像我一样有其他称为“路径”的变量,则需要重命名它们,例如:
导入“包:路径/path.dart”作为路径;
然后例如利用
final imgPath = path.join(
(await _localPath),
'${DateTime.now()}.png',
);
代替final path = join(
(await _localPath),
'${DateTime.now()}.png',
);
关于android - Flutter - 添加导入 'package:path/path.dart' 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54417811/