gpt4 book ai didi

firebase - 为什么有时imageUrl被保存在Cloud Firestore数据库中而有时却不保存

转载 作者:行者123 更新时间:2023-12-03 03:33:17 25 4
gpt4 key购买 nike

我正在创建一个flutter应用程序,该应用程序将Image-url存储在Cloud Firestore数据库中,并且以图像的形式获取该URL,并在我的flutter应用程序中显示该URL。问题在于图像URL有时会保存在数据库中,有时却不会保存。保存该文件后,提取过程将正常工作,而未保存或未保存该文件时,它将使用图像中显示的空msg接收错误值。
我不知道为什么有时会保存数据而有时却未保存数据。
请参阅以下代码,将图像保存在云Firestore数据库中。

import 'package:intl/intl.dart';
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mi_card/duplicate.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mi_card/widget/provider_widget.dart';
import 'package:path/path.dart' as path;
import 'package:firebase_storage/firebase_storage.dart';
import '../sec.dart';


class EditProductScreen extends StatefulWidget {
@override
_EditProductScreenState createState() => _EditProductScreenState();
}


class _EditProductScreenState extends State<EditProductScreen> {

//for selecting picture from galary of phone
var sampleImage;
Future captureImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.camera);

setState(() {
sampleImage = tempImage;
});
String fileName = path.basename(sampleImage.path);
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child('entry/student entry/'+fileName);
final StorageUploadTask task =
firebaseStorageRef.putFile(sampleImage);

var ImageUrl= await(await task.onComplete).ref.getDownloadURL();
url=ImageUrl.toString();
print("Image Url="+url);
//saveToDatabase(url);

}

void saveToDatabase(url){

}

//for camera opening and capturing the picture
Future getImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery);

setState(() {
sampleImage = tempImage;
});
String fileName = path.basename(sampleImage.path);
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child('entry/student entry/'+fileName);
final StorageUploadTask task =
firebaseStorageRef.putFile(sampleImage);

var ImageUrl= await(await task.onComplete).ref.getDownloadURL();
url=ImageUrl.toString();
print("Image Url="+url);
saveToDatabase(url);
}
final _priceFocusNode = FocusNode();
final _formKey = GlobalKey<FormState>();
final _firestore = Firestore.instance;
String url;






var _initValues = {
'title': '',
'description': '',
'price': '',
'imageUrl': '',
};
var _isInit = true;




@override
void didChangeDependencies() {
if (_isInit) {
final productId = ModalRoute.of(context).settings.arguments as String;

}
_isInit = false;
super.didChangeDependencies();
}

@override
void dispose() {

_priceFocusNode.dispose();

super.dispose();
}



void _saveForm() async{

final isValid = _formKey.currentState.validate();
if (!isValid) {
return;
}
_formKey.currentState.save();


var dbTimeKey = new DateTime.now();
var formatDate=new DateFormat('dd/MMMM/yyyy');
var formatTime=new DateFormat('dd/MMMM/yyyy &'' hh:mm aaa, EEEE');


String date = formatDate.format(dbTimeKey);
String time = formatTime.format(dbTimeKey);



final uid = await Provider.of(context).auth.getCurrentUID();


// collection reference for every user
DocumentReference Collection = Firestore.instance.collection(' entry').document();
Collection.setData({
"Entry-time": time,
'image': url,
});


Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyApp()),
);
}

List<String> _locations = ['NA','1st year', '2 year', '3 year', '4 year']; // Option 2
String _selectedLocation;
@override
Widget build(BuildContext context) {

var _blankFocusNode = new FocusNode();

return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('ENTRY'),
centerTitle: true,
leading: new
IconButton(
icon: Icon(Icons.arrow_back),
color: Colors.white,
onPressed: () {
Navigator.pop(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
),
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: _saveForm,
child: Text("Save",),
),

],

),
backgroundColor: Colors.blueAccent,
body: GestureDetector (
onTap: () {
FocusScope.of(context).requestFocus(_blankFocusNode);
},
child: Form(
key: _formKey,
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Align(
alignment: Alignment.center,
child: CircleAvatar(
radius:73 ,
backgroundColor: Colors.white,
child: ClipOval(
child: new SizedBox(
width: 125,
height:125,
child: sampleImage != null
? Image.file(
sampleImage,
height: 108,
fit: BoxFit.fill,
)
: IconButton(
icon: Icon(
Icons.person,
color: Colors.grey[400],
),
iconSize: 80.0,
//onPressed:_takePicture
),
),
),

),
),
Padding(
padding: EdgeInsets.only(top: 0.0),
child: IconButton(

icon: Icon(
Icons.camera_alt,
color: Colors.black,
size: 30.0,
),
onPressed: captureImage,
),

),

Padding(
padding: EdgeInsets.only(top: 00.0),
child: IconButton(
icon: Icon(
Icons.folder,
color: Colors.orangeAccent[100],
size: 30.0,
),
onPressed: getImage,
),

),
],

),

],
),
),
),

);
}
}
如何处理此错误?

最佳答案

这是将图片上传到存储并获取网址的更好方法

          final StorageReference storageReference =
FirebaseStorage().ref().child("path/$name");

final StorageUploadTask uploadTask =
storageReference.putFile(imaeFile);

final StreamSubscription<StorageTaskEvent> streamSubscription =
uploadTask.events.listen((event) {
// You can use this to notify yourself or your user in any kind of way.
// For example: you could use the uploadTask.events stream in a StreamBuilder instead
// to show your user what the current status is. In that case, you would not need to cancel any
// subscription as StreamBuilder handles this automatically.
print('EVENT ${event.type}');
});

// Cancel your subscription when done.
await uploadTask.onComplete;
streamSubscription.cancel();
String url =
await (await uploadTask.onComplete).ref.getDownloadURL();
saveToDatabase(url);

关于firebase - 为什么有时imageUrl被保存在Cloud Firestore数据库中而有时却不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62522209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com