haw*_*bee 8 future dart firebase flutter
我正在尝试用flutter和firebase构建我的第一个移动应用程序.当我尝试显示和存储照片时,我遇到以下问题:
错误:无法将参数类型"Future"分配给参数类型"File".(argument_type_not_assignable在[whereassistant] lib/main.dart:85)
我应该做一些铸造,但我不理解hox做得恰当.
这是我的Future文件声明:
Future<File> _imageFile;
Run Code Online (Sandbox Code Playgroud)
我正在拍摄一张显示在屏幕上的照片:
setState(() {
_imageFile = ImagePicker.pickImage(source: source);
});
Run Code Online (Sandbox Code Playgroud)
但是在尝试将照片发送到Firebase时出现错误:
final StorageUploadTask uploadTask = ref.put(_imageFile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
Run Code Online (Sandbox Code Playgroud)
这是我基于代码示例使用的类:
class _MyHomePageState extends State<MyHomePage> {
Future<File> _imageFile;
void _onImageButtonPressed(ImageSource source) async {
GoogleSignIn _googleSignIn = new GoogleSignIn();
var account = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await account.authentication;
final FirebaseUser user = await _auth.signInWithGoogle(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
setState(() {
_imageFile = ImagePicker.pickImage(source: source);
});
var random = new Random().nextInt(10000);
var ref = FirebaseStorage.instance.ref().child('image_$random.jpg');
final StorageUploadTask uploadTask = ref.put(_imageFile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Where Assistant'),
),
body: new Center(
child: new FutureBuilder<File>(
future: _imageFile,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
debugPrint('test recup image');
print(snapshot);
if (snapshot.connectionState == ConnectionState.done &&
snapshot.data != null) {
return new Image.file(snapshot.data);
} else if (snapshot.error != null) {
return const Text('Error picking image.');
} else {
return const Text('No image so far.');
}
},
),
),
floatingActionButton: new Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new FloatingActionButton(
onPressed: () => _onImageButtonPressed(ImageSource.gallery),
tooltip: 'Pick an image from gallery',
child: new Icon(Icons.photo_library),
),
new Padding(
padding: const EdgeInsets.only(top: 16.0),
child: new FloatingActionButton(
onPressed: () => _onImageButtonPressed(ImageSource.camera),
tooltip: 'Take a Photo',
child: new Icon(Icons.camera_alt),
),
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
ref.put
请求一个File
as参数.你传递的是一个Future<File>
.
您需要等待未来的结果才能拨打电话.
您可以将代码更改为
final StorageUploadTask uploadTask = ref.put(await _imageFile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
Run Code Online (Sandbox Code Playgroud)
或_imageFile
改为File
而不是Future<File>
归档时间: |
|
查看次数: |
18286 次 |
最近记录: |