Firebase StorageTaskSnapshot在颤振模式下不工作
我正在尝试将我的图像存储在firebase中并检索它。但我被卡住了,因为StorgeTaskSnapshot不工作,它说“未定义的类/方法”。我怎么修理它?这是由于更新的软件包版本引起的另一个问题吗 这是一张照片- 编辑:我忘了添加代码,所以我正在添加- 这是完整的文件-Firebase StorageTaskSnapshot在颤振模式下不工作,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在尝试将我的图像存储在firebase中并检索它。但我被卡住了,因为StorgeTaskSnapshot不工作,它说“未定义的类/方法”。我怎么修理它?这是由于更新的软件包版本引起的另一个问题吗 这是一张照片- 编辑:我忘了添加代码,所以我正在添加- 这是完整的文件- import 'dart:async'; //import 'dart:html'; import 'dart:io'; import 'package:cached_network_image/cached_network_
import 'dart:async';
//import 'dart:html';
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
//import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:telegramchatapp/Widgets/ProgressWidget.dart';
import 'package:telegramchatapp/main.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
bool isLoading = false;
class SettingsPage extends StatefulWidget {
@override
_SettingsPageState createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
final GoogleSignIn googleSignIn = GoogleSignIn();
Future<Null> logoutUser() async {
await FirebaseAuth.instance.signOut();
await googleSignIn.disconnect();
await googleSignIn.signOut();
setState(() {
isLoading = false;
});
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => MyApp()), (Route<dynamic> route) => false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.white,
),
backgroundColor: Colors.lightBlue,
title: Text(
'Account Settings',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
centerTitle: true,
actions: [
PopupMenuButton(
onSelected: (value) {
logoutUser();
},
itemBuilder: (BuildContext context) {
return [
PopupMenuItem(
child: Row(
children: [
Icon(
Icons.logout,
color: Colors.grey[600],
),
Text("Log out"),
],
),
value: "New Group",
),
];
},
),
],
),
body: SettingsScreen(),
);
}
}
class SettingsScreen extends StatefulWidget {
@override
State createState() => SettingsScreenState();
}
class SettingsScreenState extends State<SettingsScreen> {
TextEditingController nickNameTextEditingController;
TextEditingController aboutMeTextEditingController;
SharedPreferences preferences;
String id = "";
String nickname = "";
String aboutMe = "";
String photoUrl = "";
File imageFileAvatar;
final FocusNode nickNameFocusNode = FocusNode();
final FocusNode aboutMeFocusNode = FocusNode();
readDataFromLocal() async {
preferences = await SharedPreferences.getInstance();
id = preferences.getString("id");
nickname = preferences.getString("nickname");
aboutMe = preferences.getString("aboutMe");
photoUrl = preferences.getString("photoUrl");
nickNameTextEditingController = TextEditingController(text: nickname);
aboutMeTextEditingController = TextEditingController(text: aboutMe);
setState(() {
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
readDataFromLocal();
}
Future getImage() async {
ImagePicker tempImagePicker = ImagePicker();
final getImage = await tempImagePicker.getImage(source: ImageSource.gallery);
File newFileImage = File(getImage.path);
if(newFileImage!=null) {
setState(() {
this.imageFileAvatar = newFileImage;
isLoading = true;
});
}
uploadImageToFirestoreAndStorage();
}
uploadImageToFirestoreAndStorage() async {
String mFileName = id;
FirebaseStorage storage = FirebaseStorage.instance;
Reference storageReference = storage.ref().child(mFileName);
FirebaseStorage firebaseStorage = FirebaseStorage.instance;
UploadTask storageUploadTask = storageReference.putFile(imageFileAvatar);
Reference ref;
final newImageDownloadUrl = await ref.getDownloadURL();
photoUrl = newImageDownloadUrl;
FirebaseFirestore.instance.collection("users").doc(id).update({
"photoUrl" : photoUrl,
"aboutMe" : aboutMe,
"nickname" : nickname,
}).then((data) async {
await preferences.setString("photoUrl", photoUrl);
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Updated successfully");
});
// storageUploadTask.onComplete.then((value) {
// if(value.error==null) {
// storageTaskSnapshot = value;
// storageTaskSnapshot.ref.getDownloadUrl().then.((imageDownloadUrl) {
// photoUrl = imageDownloadUrl;
// });
// }
// });
}
void updateData() {
nickNameFocusNode.unfocus();
aboutMeFocusNode.unfocus();
setState(() {
isLoading=false;
});
FirebaseFirestore.instance.collection("users").doc(id).update({
"photoUrl" : photoUrl,
"aboutMe" : aboutMe,
"nickname" : nickname,
}).then((data) async {
await preferences.setString("photoUrl", photoUrl);
await preferences.setString("aboutMe", aboutMe);
await preferences.setString("nickname", nickname);
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Updated successfully");
});
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
child: Center(
child: Stack(
children: <Widget>[
imageFileAvatar==null
? photoUrl != ""
? Material(
child: CachedNetworkImage(
placeholder: (context, url) => Container(
child: CircularProgressIndicator(
strokeWidth: 2.0,
valueColor: AlwaysStoppedAnimation<Color>(Colors.lightBlueAccent),
),
width: 200.0,
height: 200.0,
padding: EdgeInsets.all(20.0),
),
imageUrl: photoUrl,
width: 200.0,
height: 200.0,
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(Radius.circular(125.0)),
clipBehavior: Clip.hardEdge,
)
: Icon(
Icons.account_circle, size: 90, color: Colors.grey,
)
: Material(
child: Image.file(
imageFileAvatar,
width: MediaQuery.of(context).size.width*0.5,
height: MediaQuery.of(context).size.width*0.5,
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(Radius.circular(125.0)),
clipBehavior: Clip.hardEdge,
),
Positioned(
right: MediaQuery.of(context).size.width*0.01,
bottom: MediaQuery.of(context).size.width*0.009,
child: IconButton(
icon: Icon(
Icons.camera_alt,
),
onPressed: getImage,
splashColor: Colors.transparent,
highlightColor: Colors.white,
iconSize: MediaQuery.of(context).size.width*0.1,
),
),
],
),
),
width: double.infinity,
margin: EdgeInsets.all(20.0),
),
Column(
children: [
Padding(
padding: EdgeInsets.all(10.0),
child: isLoading ? circularProgress() : Container(),
),
Container(
alignment: Alignment.centerLeft,
child: Text(
'Profile Name',
style: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.blue,
fontSize: MediaQuery.of(context).size.width*0.04,
),
),
margin: EdgeInsets.only(left: 33.0),
),
Container(
child: Theme(
data: Theme.of(context).copyWith(primaryColor: Colors.lightBlueAccent),
child: TextField(
decoration: InputDecoration(
hintText: "write a username...",
contentPadding: EdgeInsets.all(5.0),
hintStyle: TextStyle(
color: Colors.white60,
),
),
controller: nickNameTextEditingController,
onChanged: (value) {
nickname = value;
},
focusNode: nickNameFocusNode,
),
),
margin: EdgeInsets.only(left: 30.0, right: 30.0),
),
Container(
alignment: Alignment.centerLeft,
child: Text(
'Bio',
style: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.blue,
fontSize: MediaQuery.of(context).size.width*0.04,
),
),
margin: EdgeInsets.only(left: 33.0, top: 30.0,),
),
Container(
child: Theme(
data: Theme.of(context).copyWith(primaryColor: Colors.lightBlueAccent),
child: TextField(
decoration: InputDecoration(
hintText: "Write about yourself...",
contentPadding: EdgeInsets.all(5.0),
hintStyle: TextStyle(
color: Colors.white60,
),
),
controller: aboutMeTextEditingController,
onChanged: (value) {
aboutMe = value;
},
focusNode: aboutMeFocusNode,
),
),
margin: EdgeInsets.only(left: 30.0, right: 30.0),
),
],
),
SizedBox(
height: MediaQuery.of(context).size.height*0.075,
),
Container(
alignment: Alignment.center,
child: TextButton(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0, vertical: 2.0),
child: Text(
'Update',
style: TextStyle(
fontSize: MediaQuery.of(context).size.width*0.07,
color: Colors.white,
),
),
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.lightBlueAccent),
overlayColor: MaterialStateProperty.all(Colors.transparent),
),
onPressed: updateData,
),
),
],
crossAxisAlignment: CrossAxisAlignment.start,
),
),
],
);
}
}
如果更新了依赖项,则可能是使用了旧语法 你能用更新的语法上传吗。以下是一个例子:
导入'dart:io';
导入“package:firebase_storage/firebase_storage.dart”作为firebase_storage;
firebase_存储。firebase存储=
firebase_storage.FirebaseStorage.instance;
类存储服务{
静态未来上传文件(字符串路径,文件文件)异步{
试一试{
firebase_storage.Reference storageReference=
firebase_storage.FirebaseStorage.instance.ref(路径);
等待storageReference.putFile(文件);
String downloadURL=wait-storageReference.getDownloadURL();
返回下载URL;
}捕获(e){
返回null;
}
}
}
您可以这样使用它:
String photoURL=wait-StorageService.uploadFile(
'users/${u auth.currentUser.uid}/${Path.basename(\u image.Path)}',\u image);
您的函数如下所示:
uploadImageToFirestoreAndStorage()异步{
字符串mFileName=id;
Reference-storageReference=FirebaseStorage.instance.ref('path_in_storage').child(mFileName);
等待storageReference.putFile(imageFileAvatar);
字符串photoUrl=wait-storageReference.getDownloadURL();
试一试{
等待FirebaseFirestore.instance.collection(“用户”).doc(id).update({
“photoUrl”:photoUrl,
“aboutMe”:aboutMe,
“昵称”:昵称,
});
}捕获(e){
打印('e$e');
}
打印('photoUrl$photoUrl');
wait preferences.setString(“photoUrl”,photoUrl);
设置状态(){
isLoading=false;
});
flattertoast.showtoos(消息:“更新成功”);
}
这些是我从gallary开始更新个人资料图片时的日志-
I/fluttertelegra( 9924): NativeAlloc concurrent copying GC freed 6283(330KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3993KB/7987KB, paused 1.090ms total 114.133ms
D/UploadTask( 9924): Increasing chunk size to 524288
D/UploadTask( 9924): Increasing chunk size to 1048576
D/UploadTask( 9924): Increasing chunk size to 2097152
I/fluttertelegra( 9924): Background young concurrent copying GC freed 4870(2176KB) AllocSpace objects, 6(1624KB) LOS objects, 0% free, 8227KB/8227KB, paused 3.532ms total 119.502ms
I/fluttertelegra( 9924): Background concurrent copying GC freed 1139(152KB) AllocSpace objects, 1(260KB) LOS objects, 49% free, 7846KB/15MB, paused 11.888ms total 118.770ms
D/UploadTask( 9924): Increasing chunk size to 4194304
E/Toast ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
E/Toast ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
W/fluttertelegra( 9924): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
E/Toast ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
始终共享代码片段,而不是images@griffins是的,事实上我错忘了。否则我总是上传。我试图使用更新的代码,但没有成功。非常非常感谢你的帮助,但是你能告诉我我在上面代码中的错误吗,因为我的图片没有更新,而我的昵称和个人简历没有更新。这将非常有帮助。我已经更新了答案。底部是一个应该可以工作的函数版本。如果你不想上传rooth文件夹中的图片,那么只需添加一个应该上传的路径。它仍然无法正常工作。再说一次,它没有得到更新。你有任何错误吗?您是否具有写入该路径的正确权限?我已再次编辑我的帖子,您可以查看调试详细信息
I/fluttertelegra( 9924): NativeAlloc concurrent copying GC freed 6283(330KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3993KB/7987KB, paused 1.090ms total 114.133ms
D/UploadTask( 9924): Increasing chunk size to 524288
D/UploadTask( 9924): Increasing chunk size to 1048576
D/UploadTask( 9924): Increasing chunk size to 2097152
I/fluttertelegra( 9924): Background young concurrent copying GC freed 4870(2176KB) AllocSpace objects, 6(1624KB) LOS objects, 0% free, 8227KB/8227KB, paused 3.532ms total 119.502ms
I/fluttertelegra( 9924): Background concurrent copying GC freed 1139(152KB) AllocSpace objects, 1(260KB) LOS objects, 49% free, 7846KB/15MB, paused 11.888ms total 118.770ms
D/UploadTask( 9924): Increasing chunk size to 4194304
E/Toast ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
E/Toast ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used
W/fluttertelegra( 9924): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
E/Toast ( 9924): setGravity() shouldn't be called on text toasts, the values won't be used